Browse Source

Bug Fix: The index after `scroll` could be negative.

NaotoshiFujita 3 years ago
parent
commit
65fe958046

+ 2 - 9
dist/js/splide.js

@@ -1557,7 +1557,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
 
     function scroll(destination, duration, snap, callback) {
       Components2.Scroll.scroll(destination, duration, snap, function () {
-        setIndex(Move.toIndex(Move.getPosition()));
+        setIndex(loop(Move.toIndex(Move.getPosition())));
         callback && callback();
       });
     }
@@ -1968,13 +1968,6 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
     }
 
     function onEnd() {
-      var position = getPosition();
-      var index = Move.toIndex(position);
-
-      if (!between(index, 0, Splide2.length - 1)) {
-        translate(Move.shift(position, index > 0), true);
-      }
-
       set(IDLE);
       callback && callback();
       emit(EVENT_SCROLLED);
@@ -2460,7 +2453,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
     function mount() {
       init();
       on([EVENT_UPDATED, EVENT_REFRESH], init);
-      on([EVENT_MOVE, EVENT_SCROLLED], update);
+      on([EVENT_MOVE, EVENT_SCROLL, EVENT_SCROLLED], update);
     }
 
     function init() {

+ 1 - 1
src/js/components/Controller/Controller.ts

@@ -132,7 +132,7 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
    */
   function scroll( destination: number, duration?: number, snap?: boolean, callback?: AnyFunction ): void {
     Components.Scroll.scroll( destination, duration, snap, () => {
-      setIndex( Move.toIndex( Move.getPosition() ) );
+      setIndex( loop( Move.toIndex( Move.getPosition() ) ) );
       callback && callback();
     } );
   }

+ 2 - 2
src/js/components/Pagination/Pagination.ts

@@ -12,7 +12,7 @@ import {
   EVENT_MOVE,
   EVENT_PAGINATION_MOUNTED,
   EVENT_PAGINATION_UPDATED,
-  EVENT_REFRESH,
+  EVENT_REFRESH, EVENT_SCROLL,
   EVENT_SCROLLED,
   EVENT_UPDATED,
 } from '../../constants/events';
@@ -102,7 +102,7 @@ export function Pagination( Splide: Splide, Components: Components, options: Opt
   function mount(): void {
     init();
     on( [ EVENT_UPDATED, EVENT_REFRESH ], init );
-    on( [ EVENT_MOVE, EVENT_SCROLLED ], update );
+    on( [ EVENT_MOVE, EVENT_SCROLL, EVENT_SCROLLED ], update );
   }
 
   /**

+ 1 - 10
src/js/components/Scroll/Scroll.ts

@@ -5,6 +5,7 @@ import { EventInterface, RequestInterval, RequestIntervalInterface } from '../..
 import { Splide } from '../../core/Splide/Splide';
 import { AnyFunction, BaseComponent, Components, Options } from '../../types';
 import { abs, apply, between, floor, max, sign } from '../../utils';
+import { Controller } from '../Controller/Controller';
 import { BASE_VELOCITY, BOUNCE_DIFF_THRESHOLD, BOUNCE_DURATION, FRICTION_FACTOR, MIN_DURATION } from './constants';
 
 
@@ -98,16 +99,6 @@ export function Scroll( Splide: Splide, Components: Components, options: Options
    * Called when scroll ends or has been just canceled.
    */
   function onEnd(): void {
-    const position = getPosition();
-    const index    = Move.toIndex( position );
-
-    if ( ! between( index, 0, Splide.length - 1 ) ) {
-      translate( Move.shift( position, index > 0 ), true );
-    }
-
-    // todo
-    // translate( getPosition() );
-
     set( IDLE );
     callback && callback();
     emit( EVENT_SCROLLED );