forked from sarthakdev143/Amazon-Landing-Page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
39 lines (34 loc) · 927 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Retrieve DOM elements
let previous = document.querySelector(".prev");
let next = document.querySelector(".next");
let images = document.querySelectorAll(".img");
// Initialize currentIndex
let currentIndex = 0;
// Function to show current image
function showImage(index) {
images.forEach((img, i) => {
if (i === index) {
img.style.display = "block";
} else {
img.style.display = "none";
}
});
}
// Show the first image initially
showImage(currentIndex);
// Event listener for previous button
previous.addEventListener("click", () => {
currentIndex--;
if (currentIndex < 0) {
currentIndex = images.length - 1;
}
showImage(currentIndex);
});
// Event listener for next button
next.addEventListener("click", () => {
currentIndex++;
if (currentIndex >= images.length) {
currentIndex = 0;
}
showImage(currentIndex);
});