-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscript.js
More file actions
41 lines (32 loc) · 789 Bytes
/
script.js
File metadata and controls
41 lines (32 loc) · 789 Bytes
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
const imgs = document.getElementById(`imgs`);
const leftBtn = document.getElementById(`left`);
const rightBtn = document.getElementById(`right`);
const img = document.querySelectorAll(`#imgs img`);
const len = img.length;
let idx = 0;
let interval = setInterval(run, 2000);
function run() {
idx++;
changeImage();
}
function changeImage() {
if (idx > len - 1)
idx = 0;
else if (idx < 0)
idx = len - 1;
imgs.style.transform = `translateX(${-idx*550}px)`;
}
function resetInterval() {
clearInterval(interval);
interval = setInterval(run, 2000);
}
rightBtn.addEventListener('click', () => {
idx++;
changeImage();
resetInterval();
})
leftBtn.addEventListener('click', () => {
idx--;
changeImage();
resetInterval();
})