-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathapp.js
43 lines (39 loc) · 1007 Bytes
/
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
const slides = document.querySelectorAll(".slide");
const nextBtn = document.querySelector(".nextBtn");
const prevBtn = document.querySelector(".prevBtn");
slides.forEach(function (slide, index) {
slide.style.left = `${index * 100}%`;
});
let counter = 0;
nextBtn.addEventListener("click", function () {
counter++;
carousel();
});
prevBtn.addEventListener("click", function () {
counter--;
carousel();
});
function carousel() {
// working with slides
// if (counter === slides.length) {
// counter = 0;
// }
// if (counter < 0) {
// counter = slides.length - 1;
// }
// working with buttons
if (counter < slides.length - 1) {
nextBtn.style.visibility = "visible";
} else {
nextBtn.style.visibility = "hidden";
}
if (counter > 0) {
prevBtn.style.display = "block";
} else {
prevBtn.style.display = "none";
}
slides.forEach(function (slide) {
slide.style.transform = `translateX(-${counter * 100}%)`;
});
}
prevBtn.style.display = "none";