-
Notifications
You must be signed in to change notification settings - Fork 10
/
script.js
95 lines (78 loc) · 3.01 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Creator : Coding's Time
* Youtube Channel : https://www.youtube.com/channel/UC6dnKqrImGWqMb9ty1n0Ziw
* Github Profile : https://github.com/codingstime
*/
let onSlide = false;
window.addEventListener("load", () => {
autoSlide();
const dots = document.querySelectorAll(".carousel_dot");
for (let i = 0; i < dots.length; i++) {
dots[i].addEventListener("click", () => slide(i));
}
const buttonPrev = document.querySelector(".carousel_button__prev");
const buttonNext = document.querySelector(".carousel_button__next");
buttonPrev.addEventListener("click", () => slide(getItemActiveIndex() - 1));
buttonNext.addEventListener("click", () => slide(getItemActiveIndex() + 1));
})
function autoSlide() {
setInterval(() => {
slide(getItemActiveIndex() + 1);
}, 3000); // slide speed = 3s
}
function slide(toIndex) {
if (onSlide)
return;
onSlide = true;
const itemsArray = Array.from(document.querySelectorAll(".carousel_item"));
const itemActive = document.querySelector(".carousel_item__active");
const itemActiveIndex = itemsArray.indexOf(itemActive);
let newItemActive = null;
if (toIndex > itemActiveIndex) {
// check if toIndex exceeds the number of carousel items
if (toIndex >= itemsArray.length) {
toIndex = 0;
}
newItemActive = itemsArray[toIndex];
// start transition
newItemActive.classList.add("carousel_item__pos_next");
setTimeout(() => {
newItemActive.classList.add("carousel_item__next");
itemActive.classList.add("carousel_item__next");
}, 20);
} else {
// check if toIndex exceeds the number of carousel items
if (toIndex < 0) {
toIndex = itemsArray.length - 1;
}
newItemActive = itemsArray[toIndex];
// start transition
newItemActive.classList.add("carousel_item__pos_prev");
setTimeout(() => {
newItemActive.classList.add("carousel_item__prev");
itemActive.classList.add("carousel_item__prev");
}, 20);
}
// remove all transition class and switch active class
newItemActive.addEventListener("transitionend", () => {
itemActive.className = "carousel_item";
newItemActive.className = "carousel_item carousel_item__active";
onSlide = false;
}, {
once: true
});
slideIndicator(toIndex);
}
function getItemActiveIndex() {
const itemsArray = Array.from(document.querySelectorAll(".carousel_item"));
const itemActive = document.querySelector(".carousel_item__active");
const itemActiveIndex = itemsArray.indexOf(itemActive);
return itemActiveIndex;
}
function slideIndicator(toIndex) {
const dots = document.querySelectorAll(".carousel_dot");
const dotActive = document.querySelector(".carousel_dot__active");
const newDotActive = dots[toIndex];
dotActive.classList.remove("carousel_dot__active");
newDotActive.classList.add("carousel_dot__active");
}