-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.js
82 lines (71 loc) · 2.3 KB
/
slider.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
let prevBut = document.querySelector('.prev');
let nextBut = document.querySelector('.next');
let slides = document.querySelectorAll('.slide');
let toggles = document.querySelectorAll('.indicator-toggle');
function toggleClick(toggle,key) {
toggle.addEventListener('click', (event)=> {
let indexSlide;
let indexToggle;
event.preventDefault();
indexToggle = toggle.getAttribute('data-toggle');
if(!toggle.classList.contains('indicator-active')) {
toggles.forEach((item)=>{
if(item.classList.contains('indicator-active')) {
item.classList.remove('indicator-active');
}
});
toggle.classList.add('indicator-active');
}
slides.forEach((slide,key)=> {
if(slide.classList.contains('slide-active')) {
indexSlide = key + 1;
if ( indexSlide != indexToggle) {
slide.classList.remove('slide-active');
}
}
});
if ( indexSlide != indexToggle) {
slides[indexToggle-1].classList.add('slide-active');
}
if( !toggle.classList.contains('indicator-active')) {
toggle.classList.add('indicator-active');
}
});
}
prevBut.addEventListener('click', (event)=> {
event.preventDefault();
let indexSlide;
slides.forEach((slide,key)=> {
if(slide.classList.contains('slide-active')) {
slide.classList.remove('slide-active');
indexSlide = key > 0 ? key - 1 : slides.length - 1;
toggles.forEach((item)=>{
if(item.classList.contains('indicator-active')) {
item.classList.remove('indicator-active');
}
});
}
});
slides[indexSlide].classList.add('slide-active');
toggles[indexSlide].classList.add('indicator-active');
});
nextBut.addEventListener('click', (event)=> {
event.preventDefault();
let indexSlide;
slides.forEach((slide,key)=> {
if(slide.classList.contains('slide-active')) {
slide.classList.remove('slide-active');
indexSlide = key < slides.length - 1 ? key + 1 : 0;
toggles.forEach((item)=>{
if(item.classList.contains('indicator-active')) {
item.classList.remove('indicator-active');
}
});
}
});
slides[indexSlide].classList.add('slide-active');
toggles[indexSlide].classList.add('indicator-active');
});
toggles.forEach((toggle,key)=>{
toggleClick(toggle,key);
});