-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
45 lines (38 loc) · 1.02 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
// IMAGE SLIDER
const slides = document.querySelectorAll(".slides img");
let slideIndex = 0;
let intervalId = null;
document.addEventListener("DOMContentLoaded", initializeSlider);
function initializeSlider(){
if(slides.length > 0){
slides[slideIndex].classList.add("displaySlide");
intervalId = setInterval(nextSlide, 5000);
}
}
/**
* Displays the slide at the specified index.
* If the index is out of range, it wraps around to the first or last slide accordingly.
*
* @param {number} index - The index of the slide to be displayed.
*/
function showSlide(index){
if(index >= slides.length){
slideIndex = 0;
}
else if(index < 0){
slideIndex = slides.length - 1;
}
slides.forEach(slide => {
slide.classList.remove("displaySlide");
});
slides[slideIndex].classList.add("displaySlide");
}
function prevSlide(){
clearInterval(intervalId);
slideIndex--;
showSlide(slideIndex);
}
function nextSlide(){
slideIndex++;
showSlide(slideIndex);
}