Skip to content

Commit f2f4fec

Browse files
committed
refactoring
1 parent 37298eb commit f2f4fec

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

Diff for: js/script.js

+22-26
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ createPreview();
2323
let currentIndex = 0;
2424

2525
const imageItem = document.querySelectorAll(".item");
26-
imageItem[currentIndex].classList.add("active");
2726

2827
// addEventListener
2928
prevBtn.addEventListener("click", prevHandle);
@@ -57,54 +56,44 @@ function drawImage(curImage) {
5756

5857
// funzione del prevBtn
5958
function prevHandle() {
60-
imageItem[currentIndex].classList.remove("active");
61-
previewItems[currentIndex].classList.remove("active");
59+
showOff();
6260
currentIndex--;
6361
if (currentIndex < 0) {
6462
currentIndex = imageItem.length - 1;
6563
}
66-
67-
imageItem[currentIndex].classList.add("active");
68-
previewItems[currentIndex].classList.add("active");
64+
showUp();
6965
}
7066

7167
// funzione del nextBtn
7268
function nextHandle() {
73-
imageItem[currentIndex].classList.remove("active");
74-
previewItems[currentIndex].classList.remove("active");
69+
showOff();
7570
currentIndex++;
7671
if (currentIndex >= imageItem.length) {
7772
currentIndex = 0;
7873
}
79-
imageItem[currentIndex].classList.add("active");
80-
previewItems[currentIndex].classList.add("active");
81-
74+
showUp();
8275
}
8376

8477
// funzione dello slider automatico
8578
function sliderInterval() {
8679
if (autoplay & startClicked) {
87-
imageItem[currentIndex].classList.remove("active");
88-
previewItems[currentIndex].classList.remove("active");
80+
showOff();
8981
currentIndex++;
9082
if (currentIndex >= imageItem.length) {
9183
currentIndex = 0;
9284
}
93-
imageItem[currentIndex].classList.add("active");
94-
previewItems[currentIndex].classList.add("active");
85+
showUp();
9586
}
9687
}
9788

9889
function sliderIntervalInverted() {
9990
if (autoplay && invertClicked) {
100-
imageItem[currentIndex].classList.remove("active");
101-
previewItems[currentIndex].classList.remove("active");
91+
showOff();
10292
currentIndex--;
10393
if (currentIndex < 0) {
10494
currentIndex = imageItem.length - 1;
10595
}
106-
imageItem[currentIndex].classList.add("active");
107-
previewItems[currentIndex].classList.add("active");
96+
showUp();
10897
}
10998
}
11099

@@ -121,20 +110,18 @@ function createPreview() {
121110
}
122111

123112
const previewItems = document.querySelectorAll(".thumb");
124-
previewItems[currentIndex].classList.add("active");
113+
showUp();
125114
attachClickHandlers(previewItems);
126115

127116
// funzione per gestire il click sulle preview
128117
function attachClickHandlers(previewItems) {
129118
for (let i = 0; i < previewItems.length; i++) {
130119
const preview = previewItems[i];
131-
132120
preview.addEventListener("click", function () {
133121
previewItems.forEach((item) => {
134122
item.classList.remove("active");
135123
});
136124
preview.classList.add("active");
137-
138125
currentIndex = i;
139126
updateMainImage();
140127
});
@@ -169,10 +156,19 @@ function handleInvert() {
169156
}
170157
}
171158

172-
173159
// funzione per gestire lo stop button dell'autoplay
174160
function handleStopBtn() {
175-
startClicked = false;
176-
invertClicked = false;
177-
autoplay = false;
161+
clearInterval(interval);
162+
}
163+
164+
// funzione che attiva thumb e item corrent
165+
function showUp() {
166+
imageItem[currentIndex].classList.add("active");
167+
previewItems[currentIndex].classList.add("active");
168+
}
169+
170+
// funzione che nasconde thumb e item corrente
171+
function showOff() {
172+
imageItem[currentIndex].classList.remove("active");
173+
previewItems[currentIndex].classList.remove("active");
178174
}

0 commit comments

Comments
 (0)