-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliding.js
49 lines (46 loc) · 1.3 KB
/
sliding.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
{
let slides=document.querySelectorAll('.slide');
let btns=document.querySelectorAll('.btn');
let currentSlide=1;
//manual
let manualNav=function(manual){
slides.forEach((slide)=>{
slide.classList.remove('active');
});
btns.forEach((btn)=>{
btn.classList.remove('active')
});
slides[manual].classList.add('active');
btns[manual].classList.add('active');
}
btns.forEach((btn,i)=>{
btn.addEventListener("click",()=>{
manualNav(i);
currentSlide=i;
});
});
//For auto
let repeat=function(activeClass){
let active=document.getElementsByClassName('active');
let i=1;
let repeater=()=>{
setTimeout(function(){
[...active].forEach((activeSlide)=>{
activeSlide.classList.remove('active');
});
slides[i].classList.add('active');
btns[i].classList.add('active');
i++;
if(slides.length ===i){
i=0;
}
if(i>slides.length){
return;
}
repeater();
},3000);
}
repeater();
}
repeat();
}