@@ -23,7 +23,6 @@ createPreview();
23
23
let currentIndex = 0 ;
24
24
25
25
const imageItem = document . querySelectorAll ( ".item" ) ;
26
- imageItem [ currentIndex ] . classList . add ( "active" ) ;
27
26
28
27
// addEventListener
29
28
prevBtn . addEventListener ( "click" , prevHandle ) ;
@@ -57,54 +56,44 @@ function drawImage(curImage) {
57
56
58
57
// funzione del prevBtn
59
58
function prevHandle ( ) {
60
- imageItem [ currentIndex ] . classList . remove ( "active" ) ;
61
- previewItems [ currentIndex ] . classList . remove ( "active" ) ;
59
+ showOff ( ) ;
62
60
currentIndex -- ;
63
61
if ( currentIndex < 0 ) {
64
62
currentIndex = imageItem . length - 1 ;
65
63
}
66
-
67
- imageItem [ currentIndex ] . classList . add ( "active" ) ;
68
- previewItems [ currentIndex ] . classList . add ( "active" ) ;
64
+ showUp ( ) ;
69
65
}
70
66
71
67
// funzione del nextBtn
72
68
function nextHandle ( ) {
73
- imageItem [ currentIndex ] . classList . remove ( "active" ) ;
74
- previewItems [ currentIndex ] . classList . remove ( "active" ) ;
69
+ showOff ( ) ;
75
70
currentIndex ++ ;
76
71
if ( currentIndex >= imageItem . length ) {
77
72
currentIndex = 0 ;
78
73
}
79
- imageItem [ currentIndex ] . classList . add ( "active" ) ;
80
- previewItems [ currentIndex ] . classList . add ( "active" ) ;
81
-
74
+ showUp ( ) ;
82
75
}
83
76
84
77
// funzione dello slider automatico
85
78
function sliderInterval ( ) {
86
79
if ( autoplay & startClicked ) {
87
- imageItem [ currentIndex ] . classList . remove ( "active" ) ;
88
- previewItems [ currentIndex ] . classList . remove ( "active" ) ;
80
+ showOff ( ) ;
89
81
currentIndex ++ ;
90
82
if ( currentIndex >= imageItem . length ) {
91
83
currentIndex = 0 ;
92
84
}
93
- imageItem [ currentIndex ] . classList . add ( "active" ) ;
94
- previewItems [ currentIndex ] . classList . add ( "active" ) ;
85
+ showUp ( ) ;
95
86
}
96
87
}
97
88
98
89
function sliderIntervalInverted ( ) {
99
90
if ( autoplay && invertClicked ) {
100
- imageItem [ currentIndex ] . classList . remove ( "active" ) ;
101
- previewItems [ currentIndex ] . classList . remove ( "active" ) ;
91
+ showOff ( ) ;
102
92
currentIndex -- ;
103
93
if ( currentIndex < 0 ) {
104
94
currentIndex = imageItem . length - 1 ;
105
95
}
106
- imageItem [ currentIndex ] . classList . add ( "active" ) ;
107
- previewItems [ currentIndex ] . classList . add ( "active" ) ;
96
+ showUp ( ) ;
108
97
}
109
98
}
110
99
@@ -121,20 +110,18 @@ function createPreview() {
121
110
}
122
111
123
112
const previewItems = document . querySelectorAll ( ".thumb" ) ;
124
- previewItems [ currentIndex ] . classList . add ( "active" ) ;
113
+ showUp ( ) ;
125
114
attachClickHandlers ( previewItems ) ;
126
115
127
116
// funzione per gestire il click sulle preview
128
117
function attachClickHandlers ( previewItems ) {
129
118
for ( let i = 0 ; i < previewItems . length ; i ++ ) {
130
119
const preview = previewItems [ i ] ;
131
-
132
120
preview . addEventListener ( "click" , function ( ) {
133
121
previewItems . forEach ( ( item ) => {
134
122
item . classList . remove ( "active" ) ;
135
123
} ) ;
136
124
preview . classList . add ( "active" ) ;
137
-
138
125
currentIndex = i ;
139
126
updateMainImage ( ) ;
140
127
} ) ;
@@ -169,10 +156,19 @@ function handleInvert() {
169
156
}
170
157
}
171
158
172
-
173
159
// funzione per gestire lo stop button dell'autoplay
174
160
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" ) ;
178
174
}
0 commit comments