-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (37 loc) · 1.45 KB
/
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
40
41
42
43
44
45
46
function imageSlider(parent) {
let currentImage = 0;
const slider = {
parent: parent,
images: parent.querySelector(".images"),
thumbnails: parent.querySelector(".thumbnails"),
backBtn: parent.querySelector(".back-btn"),
nextBtn: parent.querySelector(".next-btn"),
};
const imageNodes = slider.images.querySelectorAll(".slider-image");
imageNodes[currentImage].classList.add("active");
slider.thumbnails.innerHTML = Array.from(imageNodes)
.map((img) => `<img src="${img.src}" />`)
.join("");
const thumbnailNodes = slider.thumbnails.querySelectorAll("img");
thumbnailNodes[currentImage].classList.add("active");
function setActiveImage(index) {
imageNodes[currentImage].classList.remove("active");
thumbnailNodes[currentImage].classList.remove("active");
currentImage = index;
imageNodes[currentImage].classList.add("active");
thumbnailNodes[currentImage].classList.add("active");
}
thumbnailNodes.forEach((thumbnail, index) => {
thumbnail.addEventListener("click", () => setActiveImage(index));
});
slider.backBtn.addEventListener("click", () => {
setActiveImage((currentImage - 1 + imageNodes.length) % imageNodes.length);
});
slider.nextBtn.addEventListener("click", () => {
setActiveImage((currentImage + 1) % imageNodes.length);
});
setInterval(() => {
setActiveImage((currentImage + 1) % imageNodes.length);
}, 3000);
}
imageSlider(document.querySelector(".image-slider"));