Skip to content
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
22 changes: 17 additions & 5 deletions Sprint-3/slideshow/index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
<!DOCTYPE html>
<!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>
<link rel="stylesheet" href="style.css" />
<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>
<h1>Giraffe Gallery</h1>
<div id="slideshow">
<button id="back">&#8592; Back</button>
<img id="slide" src="" alt="Giraffe" />
<button id="forward">Forward &#8594;</button>

<!-- automating the buttons -->
<div id="auto-controls">
<button id="auto-back">&#8592; Auto Back</button>
<button id="stop">Stop</button>
<button id="auto-forward">Auto Forward &#8594;</button>
</div>

</div>
</body>
</html>
54 changes: 50 additions & 4 deletions Sprint-3/slideshow/slideshow.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
const images = [
"./assets/cute-cat-a.png",
"./assets/cute-cat-b.jpg",
"./assets/cute-cat-c.jpg",
"https://images.unsplash.com/photo-1612358405970-e1aeba9d76c2",
"https://plus.unsplash.com/premium_photo-1664304444016-44b5d41a01eb",
"https://plus.unsplash.com/premium_photo-1661809060766-f96a928a00a5",
"https://plus.unsplash.com/premium_photo-1664302716901-63777e888686",
];

let currentIndex = 0;

// Write your code here
const slide = document.getElementById("slide");
const backButton = document.getElementById("back");
const forwardButton = document.getElementById("forward");

function updateSlide() {
slide.src = images[currentIndex];
}

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

forwardButton.addEventListener("click", () => {
currentIndex = (currentIndex + 1) % images.length;
// makes a loop so that the last pic leads to the first pic and vv
updateSlide();
});

// show first image on load
updateSlide();

let autoId;

function startAuto(direction) {
stopAuto(); // clear any existing interval first
autoId = setInterval(() => {
currentIndex = (currentIndex + direction + images.length) % images.length;
updateSlide();
}, 5000);
}

function stopAuto() {
clearInterval(autoId);
}

document.getElementById("auto-forward").addEventListener("click", () => {
startAuto(1);
});

document.getElementById("auto-back").addEventListener("click", () => {
startAuto(-1);
});

document.getElementById("stop").addEventListener("click", stopAuto);
38 changes: 37 additions & 1 deletion Sprint-3/slideshow/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
/** Write your CSS in here **/
body {
font-family: Arial, sans-serif;
text-align: center;
}

#slideshow {
display: flex;
align-items: center;
justify-content: center;
gap: 20px;
margin-top: 30px;
}

#slide {
width: 450px;
height: 300px;
object-fit: cover;
border-radius: 10px;
}

button {
padding: 10px 20px;
font-size: 1rem;
cursor: pointer;
}

/* for smartphone access */
@media (max-width: 600px) {
#slide {
width: 90vw;
height: 60vw;
}

button {
padding: 12px 18px;
}
}
Loading