-
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CYF-ITP-South Africa | Serna Malala | Module-Data-Groups | Week 3 Carousel Slideshow #286
base: main
Are you sure you want to change the base?
Changes from 4 commits
2d8f8d9
d5a6d3b
a1f3e89
48c2522
76d0b67
d5d9ea5
52270da
cfd7289
928b236
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,95 @@ const images = [ | |
]; | ||
|
||
|
||
// Write your code here | ||
// Write your code here | ||
//LEVEL 1 implementation | ||
const body = document.querySelector("body"); | ||
const image = document.getElementById("carousel-img"); | ||
const forwardButton = document.getElementById("forward-btn"); | ||
const backwardButton = document.getElementById("backward-btn"); | ||
const stopButton = document.getElementById("stop"); | ||
const autoForwardButton = document.getElementById("auto-forward-btn"); | ||
const autoBackwardButton = document.getElementById("auto-backward-btn"); | ||
|
||
let forwardsRunning = null; | ||
let backwardsRunning = null; | ||
let index = 0; | ||
image.src = images[index]; | ||
|
||
//separate moving mechanism into functions since they are used many times | ||
function moveForward(imagesArray) { | ||
if (index < imagesArray.length - 1) { | ||
index++; | ||
image.src = imagesArray[index]; | ||
|
||
} | ||
else { | ||
index = 0; | ||
image.src = imagesArray[index]; | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you think of a way (utilising the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a bit tricky to think of, I had to take array lengths and look at how modulo operator works.
|
||
|
||
} | ||
|
||
function moveBackwards(imagesArray) { | ||
if (index > 0) { | ||
index--; | ||
image.src = imagesArray[index]; | ||
|
||
} | ||
else { | ||
index = imagesArray.length - 1; | ||
image.src = imagesArray[index]; | ||
} | ||
|
||
|
||
} | ||
function carousel(imagesArray) { | ||
|
||
|
||
body.style.textAlign = "center"; | ||
image.style.width = "50%"; | ||
|
||
forwardButton.addEventListener("click", function () { | ||
moveForward(imagesArray); | ||
|
||
}) | ||
|
||
backwardButton.addEventListener("click", function () { | ||
moveBackwards(imagesArray); | ||
}) | ||
|
||
//Level 2 implementation | ||
|
||
|
||
autoForwardButton.addEventListener("click", function name() { | ||
if (forwardsRunning) { | ||
clearInterval(forwardsRunning); | ||
} | ||
|
||
forwardsRunning = setInterval(() => { | ||
moveForward(imagesArray); | ||
}, 5000); | ||
}); | ||
|
||
autoBackwardButton.addEventListener("click", function () { | ||
|
||
if (backwardsRunning) { | ||
clearInterval(backwardsRunning); | ||
} | ||
|
||
backwardsRunning = setInterval(function () { | ||
moveBackwards(imagesArray); | ||
}, 5000); | ||
|
||
}) | ||
//clear both to stop it | ||
stopButton.addEventListener("click", function () { | ||
clearInterval(backwardsRunning); | ||
clearInterval(forwardsRunning); | ||
|
||
}) | ||
} | ||
|
||
carousel(images); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use a consistent naming convention for all buttons'
id
value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add the word "btn" to stop. I understand what you mean