-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
53 lines (46 loc) · 1.62 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
const slides = document.querySelectorAll('.slider-item');
const sliderWidth = document.querySelector('.slides-wrapper').getBoundingClientRect().width;
const nextBtn = document.querySelector('.next');
const prevButton = document.querySelector('.prev');
let nextSlideIndex = 1;
let previousSlideIndex = 0;
let nextSlide = slides[nextSlideIndex];
let previousSlide = slides[previousSlideIndex];
slides.forEach((slide, index) => {
if(index !== 0) {
slide.style.left = sliderWidth * index + 'px';
}
});
//Slider Nav
(sliderNav = () => {
nextSlideIndex === 0 ? nextBtn.classList.add('btn-disable') : nextBtn.classList.remove('btn-disable');
previousSlideIndex === 0 ? prevButton.classList.add('btn-disable') : prevButton.classList.remove('btn-disable');
})();
//Next Button Click
nextBtn.onclick = () => {
nextSlide.style.left = '0px';
if(nextSlideIndex < slides.length-1) {
nextSlideIndex++;
previousSlideIndex = nextSlideIndex - 1;
} else {
nextSlideIndex = 0;
previousSlideIndex = slides.length-1;
}
sliderNav();
nextSlide = slides[nextSlideIndex];
previousSlide = slides[previousSlideIndex];
}
//Previous Button Click
prevButton.onclick = () => {
previousSlide.style.left = sliderWidth + 'px';
if(previousSlideIndex > 0) {
previousSlideIndex = previousSlideIndex - 1;
nextSlideIndex = previousSlideIndex + 1;
} else {
previousSlideIndex = 0;
nextSlideIndex = 0;
}
sliderNav();
previousSlide = slides[previousSlideIndex];
nextSlide = slides[nextSlideIndex];
}