Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
11 changes: 8 additions & 3 deletions Sprint-3/slideshow/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>Image Carousel</title>
<script defer src="slideshow.js"></script>
</head>
<body>
<img id="carousel-img" src="./assets/cute-cat-a.png" alt="cat-pic" />
<button type="button" id="backward-btn">Backwards</button>
<button type="button" id="forward-btn">Forward</button>
<div>
<button type="button" id="auto-backward-btn">Auto Backwards</button>
<button type="button" id="backward-btn">Backwards</button>
<button type="button" id="stop">Stop</button>
Copy link

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.

Copy link
Author

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

<button type="button" id="forward-btn">Forward</button>
<button type="button" id="auto-forward-btn">Auto Forward</button>
</div>
</body>
</html>
93 changes: 92 additions & 1 deletion Sprint-3/slideshow/slideshow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you think of a way (utilising the % operator) to simplify the code in lines 25-32 into 2 lines of code.

Copy link
Author

Choose a reason for hiding this comment

The 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.


    index = (index + imagesArray.length + 1) % imagesArray.length;
    image.src = imagesArray[index];


}

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);