@@ -144,23 +144,46 @@ const Mousewheel = {
144
144
if ( params . invert ) delta = - delta ;
145
145
146
146
if ( ! swiper . params . freeMode ) {
147
- if ( Utils . now ( ) - swiper . mousewheel . lastScrollTime > 60 ) {
148
- if ( delta < 0 ) {
149
- if ( ( ! swiper . isEnd || swiper . params . loop ) && ! swiper . animating ) {
150
- swiper . slideNext ( ) ;
151
- swiper . emit ( 'scroll' , e ) ;
152
- } else if ( params . releaseOnEdges ) return true ;
153
- } else if ( ( ! swiper . isBeginning || swiper . params . loop ) && ! swiper . animating ) {
154
- swiper . slidePrev ( ) ;
155
- swiper . emit ( 'scroll' , e ) ;
156
- } else if ( params . releaseOnEdges ) return true ;
147
+ // Register the new event in a variable which stores the relevant data
148
+ const newEvent = {
149
+ time : Utils . now ( ) ,
150
+ delta : Math . abs ( delta ) ,
151
+ direction : Math . sign ( delta ) ,
152
+ raw : event ,
153
+ } ;
154
+
155
+ // Keep the most recent events
156
+ const recentWheelEvents = swiper . mousewheel . recentWheelEvents ;
157
+ if ( recentWheelEvents . length >= 2 ) {
158
+ recentWheelEvents . shift ( ) ; // only store the last N events
159
+ }
160
+ const prevEvent = recentWheelEvents . length ? recentWheelEvents [ recentWheelEvents . length - 1 ] : undefined ;
161
+ recentWheelEvents . push ( newEvent ) ;
162
+
163
+ // If there is at least one previous recorded event:
164
+ // If direction has changed or
165
+ // if the scroll is quicker than the previous one:
166
+ // Animate the slider.
167
+ // Else (this is the first time the wheel is moved):
168
+ // Animate the slider.
169
+ if ( prevEvent ) {
170
+ if ( newEvent . direction !== prevEvent . direction || newEvent . delta > prevEvent . delta ) {
171
+ swiper . mousewheel . animateSlider ( newEvent ) ;
172
+ }
173
+ } else {
174
+ swiper . mousewheel . animateSlider ( newEvent ) ;
175
+ }
176
+
177
+ // If it's time to release the scroll:
178
+ // Return now so you don't hit the preventDefault.
179
+ if ( swiper . mousewheel . releaseScroll ( newEvent ) ) {
180
+ return true ;
157
181
}
158
- swiper . mousewheel . lastScrollTime = ( new window . Date ( ) ) . getTime ( ) ;
159
182
} else {
160
183
// Freemode or scrollContainer:
161
184
162
185
// If we recently snapped after a momentum scroll, then ignore wheel events
163
- // to give time for the declereration to finish. Stop ignoring after 500 msecs
186
+ // to give time for the deceleration to finish. Stop ignoring after 500 msecs
164
187
// or if it's a new scroll (larger delta or inverse sign as last event before
165
188
// an end-of-momentum snap).
166
189
const newEvent = { time : Utils . now ( ) , delta : Math . abs ( delta ) , direction : Math . sign ( delta ) } ;
@@ -261,6 +284,55 @@ const Mousewheel = {
261
284
else e . returnValue = false ;
262
285
return false ;
263
286
} ,
287
+ animateSlider ( newEvent ) {
288
+ const swiper = this ;
289
+ // If the movement is NOT big enough and
290
+ // if the last time the user scrolled was too close to the current one (avoid continuously triggering the slider):
291
+ // Don't go any further (avoid insignificant scroll movement).
292
+ if ( newEvent . delta >= 6 && Utils . now ( ) - swiper . mousewheel . lastScrollTime < 60 ) {
293
+ // Return false as a default
294
+ return true ;
295
+ }
296
+ // If user is scrolling towards the end:
297
+ // If the slider hasn't hit the latest slide or
298
+ // if the slider is a loop and
299
+ // if the slider isn't moving right now:
300
+ // Go to next slide and
301
+ // emit a scroll event.
302
+ // Else (the user is scrolling towards the beginning) and
303
+ // if the slider hasn't hit the first slide or
304
+ // if the slider is a loop and
305
+ // if the slider isn't moving right now:
306
+ // Go to prev slide and
307
+ // emit a scroll event.
308
+ if ( newEvent . direction < 0 ) {
309
+ if ( ( ! swiper . isEnd || swiper . params . loop ) && ! swiper . animating ) {
310
+ swiper . slideNext ( ) ;
311
+ swiper . emit ( 'scroll' , newEvent . raw ) ;
312
+ }
313
+ } else if ( ( ! swiper . isBeginning || swiper . params . loop ) && ! swiper . animating ) {
314
+ swiper . slidePrev ( ) ;
315
+ swiper . emit ( 'scroll' , newEvent . raw ) ;
316
+ }
317
+ // If you got here is because an animation has been triggered so store the current time
318
+ swiper . mousewheel . lastScrollTime = ( new window . Date ( ) ) . getTime ( ) ;
319
+ // Return false as a default
320
+ return false ;
321
+ } ,
322
+ releaseScroll ( newEvent ) {
323
+ const swiper = this ;
324
+ const params = swiper . params . mousewheel ;
325
+ if ( newEvent . direction < 0 ) {
326
+ if ( swiper . isEnd && ! swiper . params . loop && params . releaseOnEdges ) {
327
+ // Return true to animate scroll on edges
328
+ return true ;
329
+ }
330
+ } else if ( swiper . isBeginning && ! swiper . params . loop && params . releaseOnEdges ) {
331
+ // Return true to animate scroll on edges
332
+ return true ;
333
+ }
334
+ return false ;
335
+ } ,
264
336
enable ( ) {
265
337
const swiper = this ;
266
338
const event = Mousewheel . event ( ) ;
@@ -321,6 +393,8 @@ export default {
321
393
handle : Mousewheel . handle . bind ( swiper ) ,
322
394
handleMouseEnter : Mousewheel . handleMouseEnter . bind ( swiper ) ,
323
395
handleMouseLeave : Mousewheel . handleMouseLeave . bind ( swiper ) ,
396
+ animateSlider : Mousewheel . animateSlider . bind ( swiper ) ,
397
+ releaseScroll : Mousewheel . releaseScroll . bind ( swiper ) ,
324
398
lastScrollTime : Utils . now ( ) ,
325
399
lastEventBeforeSnap : undefined ,
326
400
recentWheelEvents : [ ] ,
0 commit comments