Skip to content

ZA Cape Town | ITP-May-2025 | Dawud Vermeulen | Sprint 3 | Slideshow #747

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 1 commit 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
26 changes: 20 additions & 6 deletions Sprint-3/slideshow/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<script defer src="slideshow.js"></script>
<title>Image carousel</title>
</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>
<h1>Image Carousel</h1>
<p>Click the buttons to navigate through the images.</p>
<main class="carousel-container">
<div class="carousel">
<button id="backward-btn">Backward</button>
<img id="carousel-img" src="" alt="Image carousel" loading="lazy"/>
<button id="forward-btn">Forward</button>
</div>
<div class="controls">
<button id="auto-forward">Auto Forward</button>
<button id="auto-backward">Auto Backward</button>
<button id="stop">Stop Auto</button>
<label for="delayInput">Delay (seconds):</label>
<input type="number" id="delayInput" min="0.1" step="0.1" value="2" />

Choose a reason for hiding this comment

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

Solid approach, but are we using this input anywhere?
How can we ensure that the delay takes the value passed here into consideration for auto-forward and backwards?

</div>
</main>

<script src="slideshow.js"></script>
</body>
</html>
</html>
68 changes: 64 additions & 4 deletions Sprint-3/slideshow/slideshow.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
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");

const images = [

Choose a reason for hiding this comment

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

Good use of local resources, but the requirements mention "a list of at least 4 images", and we only have 3 here.

Bonus points and completely optional: Can you find a way to replace static local images with linked ones from a site like upsplash?

"./assets/cute-cat-a.png",
"./assets/cute-cat-b.jpg",
"./assets/cute-cat-c.jpg",
"./assets/cute-cat-a.png",
"./assets/cute-cat-b.jpg",
"./assets/cute-cat-c.jpg",
];

let currentIndex = 0;

let autoPlayIntervalId;
const intervalTime = 2000;

function updateImage() {
carouselImg.src = images[currentIndex];
}

function setAutoPlayButtonState(disabled) {
autoForwardBtn.disabled = disabled;
autoBackwardBtn.disabled = disabled;
forwardBtn.disabled = disabled;
backwardBtn.disabled = disabled;
}

function startAutoForward() {
stopAutoPlay();
setAutoPlayButtonState(true);
autoPlayIntervalId = setInterval(() => {
currentIndex = (currentIndex + 1) % images.length;
updateImage();
}, intervalTime);
}

function startAutoBackward() {
stopAutoPlay();
setAutoPlayButtonState(true);
autoPlayIntervalId = setInterval(() => {
currentIndex = (currentIndex - 1 + images.length) % images.length;
updateImage();
}, intervalTime);
}

function stopAutoPlay() {
clearInterval(autoPlayIntervalId);
setAutoPlayButtonState(false);
}

forwardBtn.addEventListener("click", () => {
currentIndex = (currentIndex + 1) % images.length;
updateImage();
});

backwardBtn.addEventListener("click", () => {
currentIndex = (currentIndex - 1 + images.length) % images.length;
updateImage();
});

autoForwardBtn.addEventListener("click", startAutoForward);
autoBackwardBtn.addEventListener("click", startAutoBackward);
stopBtn.addEventListener("click", stopAutoPlay);

// Write your code here
updateImage();