forked from solygambas/html-css-javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
45 lines (41 loc) · 1.18 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
const slides = document.querySelectorAll(".slide");
const nextButton = document.getElementById("next");
const prevButton = document.getElementById("prev");
const auto = true;
const intervalTime = 5000;
let slideInterval;
const nextSlide = () => {
const current = document.querySelector(".current");
current.classList.remove("current");
if (current.nextElementSibling) {
current.nextElementSibling.classList.add("current");
} else {
slides[0].classList.add("current");
}
};
const prevSlide = () => {
const current = document.querySelector(".current");
current.classList.remove("current");
if (current.previousElementSibling) {
current.previousElementSibling.classList.add("current");
} else {
slides[slides.length - 1].classList.add("current");
}
};
nextButton.addEventListener("click", () => {
nextSlide();
if (auto) {
clearInterval(slideInterval);
slideInterval = setInterval(nextSlide, intervalTime);
}
});
prevButton.addEventListener("click", () => {
prevSlide();
if (auto) {
clearInterval(slideInterval);
slideInterval = setInterval(nextSlide, intervalTime);
}
});
if (auto) {
slideInterval = setInterval(nextSlide, intervalTime);
}