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
Binary file added Sprint-3/slideshow/assets/cute-cat-d.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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-btn">Stop</button>
<button type="button" id="forward-btn">Forward</button>
<button type="button" id="auto-forward-btn">Auto Forward</button>
</div>
</body>
</html>
84 changes: 83 additions & 1 deletion Sprint-3/slideshow/slideshow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,89 @@ const images = [
"./assets/cute-cat-a.png",
"./assets/cute-cat-b.jpg",
"./assets/cute-cat-c.jpg",
"./assets/cute-cat-d.jpg",
];


// 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-btn");
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) {
//to get 0 as an index (index + imagesArray.length)%imagesArray.length
//increment by 1 to move
index = (index + imagesArray.length + 1) % imagesArray.length;
image.src = imagesArray[index];

}

function moveBackwards(imagesArray) {

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


}
function carousel(imagesArray) {


body.style.textAlign = "center";
image.style.height = "300px";

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 = null;
}

forwardsRunning = setInterval(() => {
moveForward(imagesArray);
}, 5000);
});

autoBackwardButton.addEventListener("click", function () {

if (backwardsRunning) {
clearInterval(backwardsRunning);
backwardsRunning = null;
}

backwardsRunning = setInterval(function () {
moveBackwards(imagesArray);
}, 5000);

})
//clear both to stop it
stopButton.addEventListener("click", function () {
clearInterval(backwardsRunning);
clearInterval(forwardsRunning);

})
}

carousel(images);