-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
40 lines (34 loc) · 923 Bytes
/
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
const prevBtn = document.querySelector('.prev');
const nextBtn = document.querySelector('.next');
const slides = document.querySelectorAll('.slide');
let index = 0;
const displaySlide = (index) => {
// Hide slides
slides.forEach((slide) => {
slide.style.display = 'none';
});
// Display next or prev slide
slides[index].style.display = 'flex';
};
const nextSlide = () => {
index++;
// Check it index is above max
if (index > slides.length - 1) {
// Reset index
index = 0;
}
displaySlide(index);
};
const prevSlide = () => {
index--;
// Check if index is below beginning
if (index < 0) {
// Set index to last slide
index = slides.length - 1;
}
displaySlide(index);
};
nextBtn.addEventListener('click', nextSlide);
prevBtn.addEventListener('click', prevSlide);
// Only display first slide initially
displaySlide(index);