-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (85 loc) · 2.76 KB
/
Copy pathscript.js
File metadata and controls
100 lines (85 loc) · 2.76 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const imagesCont = document.querySelector(".images");
const images = Array.from(imagesCont.children);
let currentIndex = 0;
let autoSlideInterval;
const popUp = false;
function setInitialClasses() {
images.forEach((img, index) => {
img.className = "";
if (index === currentIndex) img.classList.add("center");
else if (index === (currentIndex + 1) % images.length)
img.classList.add("right");
else if (index === (currentIndex - 1 + images.length) % images.length)
img.classList.add("left");
else img.classList.add("back");
});
}
setInitialClasses();
const selectorsCont = document.querySelector(".selectors");
let firstDone = false;
for (let i = 0; i < images.length; i++) {
const selector = document.createElement("div");
selector.classList.add("selector");
!firstDone && selector.classList.add("selected");
selectorsCont.appendChild(selector);
firstDone = true;
}
const selectors = Array.from(selectorsCont.children);
function updateClasses(newIndex) {
currentIndex = newIndex;
setInitialClasses();
selectors.forEach((selector, index) => {
selector.classList.remove("selected");
if (index === currentIndex) selector.classList.add("selected");
});
resetAutoSlide();
}
function autoSlide() {
updateClasses((currentIndex + 1) % images.length);
}
function resetAutoSlide() {
clearInterval(autoSlideInterval);
autoSlideInterval = setInterval(autoSlide, 7000);
}
document
.getElementById("left")
.addEventListener("click", () =>
updateClasses((currentIndex - 1 + images.length) % images.length)
);
document
.getElementById("right")
.addEventListener("click", () =>
updateClasses((currentIndex + 1) % images.length)
);
images.forEach((img, index) => {
img.addEventListener("click", () => {
if (img.classList.contains("left")) {
updateClasses((currentIndex - 1 + images.length) % images.length);
} else if (img.classList.contains("right")) {
updateClasses((currentIndex + 1) % images.length);
} else if (img.classList.contains("center")) {
const popUpDiv = document.createElement("div");
popUpDiv.classList.add("popup");
const popUpImg = document.createElement("img");
popUpImg.src = img.src;
popUpDiv.appendChild(popUpImg);
document.body.appendChild(popUpDiv);
popUpDiv.addEventListener("click", (e) => {
if (e.target !== popUpImg) {
popUpDiv.classList.add("fade-out");
popUpDiv.addEventListener("animationend", () => {
document.body.removeChild(popUpDiv);
resetAutoSlide();
});
}
});
clearInterval(autoSlideInterval);
}
});
});
selectors.forEach((selector, index) => {
selector.addEventListener("click", () => {
updateClasses(index);
});
});
resetAutoSlide();