-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.js
More file actions
39 lines (34 loc) · 1.11 KB
/
app2.js
File metadata and controls
39 lines (34 loc) · 1.11 KB
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
let slider = document.querySelector('.slider .list');
let items = document.querySelectorAll('.slider .list .item');
let next = document.getElementById('next');
let prev = document.getElementById('prev');
let dots = document.querySelectorAll('.slider .dots li');
let lengthItems = items.length - 1;
let active = 0;
next.onclick = function(){
active = active + 1 <= lengthItems ? active + 1 : 0;
reloadSlider();
}
prev.onclick = function(){
active = active - 1 >= 0 ? active - 1 : lengthItems;
reloadSlider();
}
let refreshInterval = setInterval(()=> {next.click()}, 3000);
function reloadSlider(){
slider.style.left = -items[active].offsetLeft + 'px';
//
let last_active_dot = document.querySelector('.slider .dots li.active');
last_active_dot.classList.remove('active');
dots[active].classList.add('active');
clearInterval(refreshInterval);
refreshInterval = setInterval(()=> {next.click()}, 3000);
}
dots.forEach((li, key) => {
li.addEventListener('click', ()=>{
active = key;
reloadSlider();
})
})
window.onresize = function(event) {
reloadSlider();
};