forked from codestates-seb/daily-life-of-codestates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (46 loc) · 1.08 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
const intervalID = [];
const showAction = (idx) => {
const imgs = document.querySelectorAll(".imgs__container img");
if (imgs.length > idx && idx >= 0) {
for (let i = 0; i < imgs.length; i += 1) {
if (i === idx) {
imgs[i].style.display = "inline-block";
} else {
imgs[i].style.display = "none";
}
}
}
};
const repeatAction = (idx) => {
let intervalIdx = idx;
let id;
id = setInterval(() => {
if (intervalIdx === idx - 1) {
clearInterval(id);
}
if (intervalIdx >= idx) {
intervalIdx = 0;
} else {
intervalIdx += 1;
}
showAction(intervalIdx);
}, 300);
intervalID.push(id);
};
const stopInterval = (id) => {
clearInterval(id);
}
const actions = document.querySelectorAll(".actions__container > button");
for (let i = 0; i < actions.length; i += 1) {
actions[i].addEventListener("click", () => {
if (i === 3) {
repeatAction(i);
} else {
intervalID.forEach(id => clearInterval(id));
showAction(i);
}
});
}
window.addEventListener("load", () => {
showAction(0);
});