-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (42 loc) · 1.52 KB
/
index.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
const container = document.querySelector(".container");
const imgContainer = document.querySelector(".img-container");
container.addEventListener("click", clickHandler);
imgContainer.addEventListener("scroll", scrollHandler);
let active = 1;
function clickHandler(e) {
const id = e.target.id;
const clientWidth = imgContainer.clientWidth;
if (id === "left") {
imgContainer.scrollBy(-clientWidth, 0);
if (active > 1) active--;
} else if (id === "right") {
imgContainer.scrollBy(clientWidth, 0);
if (active < 6) active++;
} else {
const numId = Number(id.split("-")[1]);
if (numId >= 1 && numId <= 6) {
active = numId;
imgContainer.scroll(clientWidth * (active - 1), 0);
}
}
}
function scrollHandler() {
const activeElement = document.querySelector(`.active-circle`);
if (activeElement) activeElement.classList.remove("active-circle");
const activeImg = document.querySelector(".active-img");
if (activeImg) activeImg.classList.remove("active-img");
active = Math.ceil(imgContainer.scrollLeft / imgContainer.clientWidth);
if (active > 6) active = 6;
const newActiveElem = document.getElementById(`circle-${active}`);
if (newActiveElem) newActiveElem.classList.add("active-circle");
const newActiveImg = document.getElementById(`img-${active}`);
if (newActiveImg) newActiveImg.classList.add("active-img");
}
setInterval(() => {
if(active < 6) {
imgContainer.scrollBy(imgContainer.clientWidth, 0);
}else {
imgContainer.scroll(0, 0);
}
scrollHandler();
}, 3000);