-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
52 lines (42 loc) · 1.43 KB
/
carousel.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
let slidePosition = 0;
const slides = document.getElementsByClassName('carousel_item');
const totalSlides = slides.length;
// const dotsNav = document.querySelector('.carousel_nav');
document.getElementById('carousel_button--next').addEventListener("click", function() {
moveToNextSlide();
});
document.getElementById('carousel_button--prev').addEventListener("click", function() {
moveToPrevSlide();
});
function updateSlidePosition() {
for (let slide of slides) {
slide.classList.remove('carousel_item--visible');
slide.classList.add('carousel_item--hidden');
}
slides[slidePosition].classList.add('carousel_item--visible');
}
function moveToNextSlide() {
if (slidePosition === totalSlides - 1) {
slidePosition = 0;
} else {
slidePosition++;
}
updateSlidePosition();
}
function moveToPrevSlide() {
if (slidePosition === 0) {
slidePosition = totalSlides - 1;
} else {
slidePosition--;
}
updateSlidePosition();
}
// dotsNav.addEventListener('click', e => {
// const targetDot = e.target.closest('button');
// if (!targetDot) return;
// const currentSlide = track.querySelector('.current-slide');
// const currentDot = dotsNav.querySelector('.current-slide');
// const targetIndex = dots.findIndex(dot => dot === targetDot);
// const targetSlide = slides[targetIndex];
// moveToSlide(track, currentSlide, targetSlide);
// })