Skip to content

ZA | 25-ITP-May | Malusi Skunyana | Sprint 3 | Slideshow #761

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sprint-3/slideshow/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.pdf
*.docx
prep/
package-lock.json
../package-lock.json
../../Sprint-1/package-lock.json
../../Sprint-2/package-lock.json
../alarmclock/package.json
Binary file added Sprint-3/slideshow/images/puppy1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sprint-3/slideshow/images/puppy2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sprint-3/slideshow/images/puppy3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sprint-3/slideshow/images/puppy4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 22 additions & 6 deletions Sprint-3/slideshow/index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image carousel</title>
<script defer src="slideshow.js"></script>
<link rel="stylesheet" href="style.css">
</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>
<img id="carousel-img" src="./images/puppy1.jpg" alt="puppy-pic">

<section aria-label="Slideshow controls">
<h2>Slideshow Controls</h2>

<!-- Manual navigation buttons -->
<div class="controls">
<button id="backward-btn">Backwards</button>
<button id="forward-btn">Forward</button>
</div>

<!-- Automatic navigation buttons -->
<div class="controls">
<button id="auto-backward">Auto Backwards</button>
<button id="auto-forward">Auto Forward</button>
<button id="stop">Stop</button>
</div>
</section>
</body>
</html>
66 changes: 62 additions & 4 deletions Sprint-3/slideshow/slideshow.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
const images = [
"./assets/cute-cat-a.png",
"./assets/cute-cat-b.jpg",
"./assets/cute-cat-c.jpg",
"./images/puppy1.jpg",
"./images/puppy2.jpg",
"./images/puppy3.jpg",
"./images/puppy4.jpg",
];

let currentIndex = 0;
let intervalId = null;
const intervalTime = 2000; // 2 seconds

// Write your code here
// Get DOM elements
const carouselImg = document.getElementById("carousel-img");
const forwardBtn = document.getElementById("forward-btn");
const backwardBtn = document.getElementById("backward-btn");
const autoForwardBtn = document.getElementById("auto-forward");
const autoBackwardBtn = document.getElementById("auto-backward");
const stopBtn = document.getElementById("stop");

// Show image helper
function showImage(index) {
carouselImg.src = images[index];
}

// Stop auto-advance
function stopAuto() {
clearInterval(intervalId);
intervalId = null;
autoForwardBtn.disabled = false;
autoBackwardBtn.disabled = false;
}

// Manual navigation
function nextImage() {
if (intervalId) stopAuto(); // stop auto if running
currentIndex = (currentIndex + 1) % images.length;
showImage(currentIndex);
}

function prevImage() {
if (intervalId) stopAuto(); // stop auto if running
currentIndex = (currentIndex - 1 + images.length) % images.length;
showImage(currentIndex);
}

forwardBtn.addEventListener("click", nextImage);
backwardBtn.addEventListener("click", prevImage);

// Auto navigation
function startAuto(directionFn) {
autoForwardBtn.disabled = true;
autoBackwardBtn.disabled = true;
intervalId = setInterval(directionFn, intervalTime);
}

autoForwardBtn.addEventListener("click", () => startAuto(() => {
currentIndex = (currentIndex + 1) % images.length;
showImage(currentIndex);
}));

autoBackwardBtn.addEventListener("click", () => startAuto(() => {
currentIndex = (currentIndex - 1 + images.length) % images.length;
showImage(currentIndex);
}));

Choose a reason for hiding this comment

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

NTH/ Not necessary to do: remove duplication

This code is very similar to the prevImage/nextImage code. In your original PR you reused these methods for the auto functionality, which was good.

Suggestion: You have already seen how you can wrap a function around another to add to it (your startAuto function is a wrapper). Perhaps you could use the same idea for your manual navigation functionality.


stopBtn.addEventListener("click", stopAuto);

Choose a reason for hiding this comment

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

Good job separating concerns! The individual functions make the code more readable.

Copy link
Author

Choose a reason for hiding this comment

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

I appreciate all the great feedback you have given.

Loading