-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (40 loc) · 1.08 KB
/
app.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
const prev = document.getElementById("prev");
const next = document.getElementById("next");
const images = [...document.getElementsByTagName("img")];
const dots = [...document.querySelectorAll(".dot")];
let position = 0;
prev.addEventListener("click", () => {
updateCarousel(() => updateImages("prev"));
});
next.addEventListener("click", () => {
updateCarousel(() => updateImages("next"));
});
function updateCarousel(callback) {
images[position].classList.remove("active");
dots[position].classList.remove("dot-active");
callback();
images[position].classList.add("active");
dots[position].classList.add("dot-active");
}
function updateImages(direction) {
switch (direction) {
case "next":
if (position == images.length - 1) {
position = 0;
} else position++;
break;
case "prev":
if (position == 0) {
position = images.length - 1;
} else position--;
break;
}
}
/* Dots Click */
dots.forEach((dot, index) => {
dot.addEventListener("click", () => {
updateCarousel(() => {
position = index;
});
});
});