-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (46 loc) · 1.24 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
54
let items = document.querySelectorAll('.slider .list .item');
let next = document.getElementById('next');
let prev = document.getElementById('prev');
let dots = document.querySelectorAll('.thumbnail .dot');
// config param
let countItem = items.length;
let itemActive = 0;
// event next click
next.onclick = function() {
itemActive = itemActive + 1;
if (itemActive >= countItem) {
itemActive = 0;
}
showSlider();
}
// event prev click
prev.onclick = function() {
itemActive = itemActive - 1;
if (itemActive < 0) {
itemActive = countItem - 1;
}
showSlider();
}
// auto run slider
let refreshInterval = setInterval(() => {
next.click();
}, 5000);
function showSlider() {
// remove item active old
let itemActiveOld = document.querySelector('.slider .list .item.active');
itemActiveOld.classList.remove('active');
// active new item
items[itemActive].classList.add('active');
// clear auto time run slider
clearInterval(refreshInterval);
refreshInterval = setInterval(() => {
next.click();
}, 5000);
}
// click dot to change slide
dots.forEach((dot, index) => {
dot.addEventListener('click', () => {
itemActive = index;
showSlider();
});
});