1
+ let slides , thumbnails ;
2
+ let prevSlide , nextSlide ;
3
+ let prevThumbnail , nextThumbnail ;
4
+ let currentSlideIndex = 1 ;
5
+ let interval = 5000 ;
6
+ let timer ;
7
+ let windowEvent ;
8
+ let thumbnailDiv , thumbnailWidth , eachThumbnailWidth , scrollToWidth ;
9
+ let thumbnailShowAtATime = 5 ;
10
+ let dataHasPrevTrumbnail = 0 ;
11
+ let dataHasNextTrumbnail = 0 ;
12
+ window . addEventListener ( 'load' , function ( e ) {
13
+ windowEvent = e ;
14
+ slides = document . getElementsByClassName ( 'slide' ) ;
15
+ if ( document . getElementsByClassName ( 'carousel-thumbnail-inner' ) ) {
16
+ thumbnailDiv = document . getElementsByClassName ( 'carousel-thumbnail-inner' ) [ 0 ] ;
17
+ }
18
+ if ( document . getElementsByClassName ( 'carousel-thumbnail-inner' ) ) {
19
+ if ( document . getElementsByClassName ( 'carousel-thumbnail-inner' ) . length > 0 ) {
20
+ thumbnails = document . getElementsByClassName ( 'thumbnail' ) ;
21
+ if ( thumbnails ) {
22
+ for ( let indicatorIndex = 0 ; indicatorIndex < thumbnails . length ; indicatorIndex ++ ) {
23
+ thumbnails [ indicatorIndex ] . addEventListener ( 'click' , function ( e ) {
24
+ goToCurrentSlide ( indicatorIndex + 1 , false , e ) ;
25
+ } ) ;
26
+ thumbnails [ indicatorIndex ] . addEventListener ( 'keydown' , function ( e ) {
27
+ goToCurrentSlide ( indicatorIndex + 1 , true , e ) ;
28
+ } ) ;
29
+ }
30
+ }
31
+ }
32
+ }
33
+ if ( thumbnailDiv && ( thumbnails && thumbnails . length > 0 ) ) {
34
+ thumbnailWidth = document . getElementsByClassName ( 'carousel-thumbnail-inner' ) [ 0 ] . offsetWidth ;
35
+ setThumbnailImg ( thumbnails ) ;
36
+ dataHasNextTrumbnail = thumbnails . length - thumbnailShowAtATime ;
37
+ }
38
+ if ( document . getElementsByClassName ( 'prevSlide' ) ) {
39
+ prevSlide = document . getElementsByClassName ( 'prevSlide' ) [ 0 ] ;
40
+ prevSlide . addEventListener ( 'click' , function ( e ) {
41
+ goToPrevSlide ( false , e ) ;
42
+ } ) ;
43
+ prevSlide . addEventListener ( 'keydown' , function ( e ) {
44
+ goToPrevSlide ( true , e ) ;
45
+ } ) ;
46
+ }
47
+ if ( document . getElementsByClassName ( 'nextSlide' ) ) {
48
+ nextSlide = document . getElementsByClassName ( 'nextSlide' ) [ 0 ] ;
49
+ nextSlide . addEventListener ( 'click' , function ( e ) {
50
+ goToNextSlide ( false , e ) ;
51
+ } ) ;
52
+ nextSlide . addEventListener ( 'keydown' , function ( e ) {
53
+ goToNextSlide ( true , e ) ;
54
+ } ) ;
55
+ }
56
+ if ( document . getElementsByClassName ( 'thumbnail-prev' ) ) {
57
+ prevThumbnail = document . getElementsByClassName ( 'thumbnail-prev' ) [ 0 ] ;
58
+ prevThumbnail . addEventListener ( 'click' , function ( e ) {
59
+ leftScrollThumbnail ( false , e ) ;
60
+ } ) ;
61
+ prevThumbnail . addEventListener ( 'keydown' , function ( e ) {
62
+ leftScrollThumbnail ( true , e ) ;
63
+ } ) ;
64
+ }
65
+ if ( document . getElementsByClassName ( 'thumbnail-next' ) ) {
66
+ nextThumbnail = document . getElementsByClassName ( 'thumbnail-next' ) [ 0 ] ;
67
+ nextThumbnail . addEventListener ( 'click' , function ( e ) {
68
+ rightScrollThumbnail ( false , e ) ;
69
+ } ) ;
70
+ nextThumbnail . addEventListener ( 'keydown' , function ( e ) {
71
+ rightScrollThumbnail ( true , e ) ;
72
+ } ) ;
73
+ }
74
+ if ( slides && slides . length > 1 ) {
75
+ currentSlideIndex = getCurrentSlideIndex ( ) ;
76
+ setControlButton ( currentSlideIndex ) ;
77
+ autoPlay ( windowEvent , interval ) ;
78
+ }
79
+ if ( prevThumbnail && nextThumbnail ) {
80
+ currentSlideIndex = getCurrentSlideIndex ( ) ;
81
+ setThumnailControlButton ( ) ;
82
+ }
83
+ } ) ;
84
+ function setControlButton ( currentSlideIndex ) {
85
+ if ( currentSlideIndex == 1 ) {
86
+ prevSlide . classList . add ( 'inactive' ) ;
87
+ } else {
88
+ prevSlide . classList . remove ( 'inactive' ) ;
89
+ }
90
+ if ( currentSlideIndex == slides . length ) {
91
+ nextSlide . classList . add ( 'inactive' ) ;
92
+ } else {
93
+ nextSlide . classList . remove ( 'inactive' ) ;
94
+ }
95
+ }
96
+ function goToPrevSlide ( isKey , event ) {
97
+ currentSlideIndex = getCurrentSlideIndex ( ) - 1 ;
98
+ if ( currentSlideIndex >= 1 ) {
99
+ if ( isKey ) {
100
+ if ( event . keyCode === 13 ) {
101
+ showCurrentSlide ( currentSlideIndex ) ;
102
+ }
103
+ } else {
104
+ showCurrentSlide ( currentSlideIndex ) ;
105
+ }
106
+ setControlButton ( currentSlideIndex ) ;
107
+ }
108
+ }
109
+ function goToNextSlide ( isKey , event ) {
110
+ currentSlideIndex = getCurrentSlideIndex ( ) + 1 ;
111
+ if ( currentSlideIndex <= slides . length ) {
112
+ if ( isKey ) {
113
+ if ( event . keyCode === 13 ) {
114
+ showCurrentSlide ( currentSlideIndex ) ;
115
+ }
116
+ } else {
117
+ showCurrentSlide ( currentSlideIndex ) ;
118
+ }
119
+ setControlButton ( currentSlideIndex ) ;
120
+ }
121
+ }
122
+ function getCurrentSlideIndex ( ) {
123
+ for ( let slideIndex = 0 ; slideIndex < slides . length ; slideIndex ++ ) {
124
+ if ( slides [ slideIndex ] . classList . contains ( 'active' ) ) {
125
+ currentSlideIndex = slideIndex ;
126
+ }
127
+ }
128
+ return currentSlideIndex + 1 ;
129
+ }
130
+ function goToCurrentSlide ( slideIndex , isKey , event ) {
131
+ if ( isKey ) {
132
+ if ( event . keyCode === 13 ) {
133
+ showCurrentSlide ( slideIndex ) ;
134
+ }
135
+ } else {
136
+ showCurrentSlide ( slideIndex ) ;
137
+ }
138
+ }
139
+ function showCurrentSlide ( slideIndex ) {
140
+ if ( slides && slides . length > 0 ) {
141
+ for ( let slide of slides ) {
142
+ slide . classList . remove ( 'active' ) ;
143
+ }
144
+ slides [ slideIndex - 1 ] . classList . add ( 'active' ) ;
145
+ }
146
+ if ( thumbnails && thumbnails . length > 0 ) {
147
+ for ( let indicator of thumbnails ) {
148
+ indicator . classList . remove ( 'active' ) ;
149
+ indicator . setAttribute ( 'aria-disabled' , 'true' ) ;
150
+ }
151
+ thumbnails [ slideIndex - 1 ] . classList . add ( 'active' ) ;
152
+ thumbnails [ slideIndex - 1 ] . setAttribute ( 'aria-disabled' , 'false' ) ;
153
+ }
154
+ if ( slideIndex > thumbnailShowAtATime ) {
155
+ rightScrollThumbnail ( ) ;
156
+ }
157
+ if ( slideIndex === thumbnails . length ) {
158
+ dataHasNextTrumbnail = 0 ;
159
+ dataHasPrevTrumbnail = thumbnails . length - thumbnailShowAtATime ;
160
+ }
161
+ if ( slideIndex === 1 ) {
162
+ dataHasPrevTrumbnail = 0 ;
163
+ dataHasNextTrumbnail = thumbnails . length - thumbnailShowAtATime ;
164
+ thumbnailDiv . scrollLeft = 0 ;
165
+ }
166
+ setThumnailControlButton ( ) ;
167
+ setControlButton ( slideIndex ) ;
168
+ clearInterval ( timer ) ;
169
+ autoPlay ( windowEvent , interval ) ;
170
+ }
171
+ function autoPlay ( event , interval ) {
172
+ if ( interval === 0 ) return ;
173
+ timer = setInterval ( function ( ) {
174
+ if ( currentSlideIndex <= slides . length ) {
175
+ goToNextSlide ( false , event ) ;
176
+ }
177
+ if ( currentSlideIndex > slides . length ) {
178
+ currentSlideIndex = 1 ;
179
+ showCurrentSlide ( currentSlideIndex ) ;
180
+ }
181
+ } , interval ) ;
182
+ }
183
+ function setThumnailControlButton ( ) {
184
+ if ( dataHasPrevTrumbnail === 0 ) {
185
+ prevThumbnail . classList . remove ( 'enable' ) ;
186
+ } else {
187
+ prevThumbnail . classList . add ( 'enable' ) ;
188
+ }
189
+ if ( dataHasNextTrumbnail === 0 ) {
190
+ nextThumbnail . classList . remove ( 'enable' ) ;
191
+ } else {
192
+ nextThumbnail . classList . add ( 'enable' ) ;
193
+ }
194
+ }
195
+ function setThumbnailImg ( thumbnails ) {
196
+ for ( let thumbnail of thumbnails ) {
197
+ eachThumbnailWidth = thumbnailWidth / thumbnailShowAtATime ;
198
+ thumbnail . childNodes [ 1 ] . children [ 0 ] . width = eachThumbnailWidth ;
199
+ }
200
+ }
201
+ function rightScrollThumbnail ( isKey , event ) {
202
+ thumbnailDiv . scrollLeft += eachThumbnailWidth ;
203
+ dataHasNextTrumbnail -- ;
204
+ dataHasPrevTrumbnail ++ ;
205
+ if ( isKey ) {
206
+ if ( event . keyCode === 13 ) {
207
+ setThumnailControlButton ( ) ;
208
+ }
209
+ } else {
210
+ setThumnailControlButton ( ) ;
211
+ }
212
+ }
213
+ function leftScrollThumbnail ( isKey , event ) {
214
+ thumbnailDiv . scrollLeft -= eachThumbnailWidth ;
215
+ dataHasNextTrumbnail ++ ;
216
+ dataHasPrevTrumbnail -- ;
217
+ if ( isKey ) {
218
+ if ( event . keyCode === 13 ) {
219
+ setThumnailControlButton ( ) ;
220
+ }
221
+ } else {
222
+ setThumnailControlButton ( ) ;
223
+ }
224
+ }
0 commit comments