Skip to content

Commit 8aff099

Browse files
Bug Fix: Rarely the loop carousel rewound itself.
1 parent 85c0c5a commit 8aff099

11 files changed

Lines changed: 51 additions & 58 deletions

File tree

dist/js/splide.cjs.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* Splide.js
3-
* Version : 4.0.4
3+
* Version : 4.0.5
44
* License : MIT
55
* Copyright: 2022 Naotoshi Fujita
66
*/
@@ -1428,11 +1428,15 @@ function Move(Splide2, Components2, options) {
14281428

14291429
function move(dest, index, prev, callback) {
14301430
var position = getPosition();
1431-
var crossing = sign(dest - prev) * orient(toPosition(dest) - position) < 0;
1431+
var shifted = shift(position, dest > prev);
1432+
var oriented = orient(shifted);
1433+
var destination = toPosition(dest);
1434+
var shouldShift = dest !== index || abs(shifted - destination) < abs(position - destination);
1435+
var canShift = dest > prev ? oriented >= 0 : oriented <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
14321436

1433-
if ((dest !== index || crossing) && canShift(dest > prev)) {
1437+
if (shouldShift && canShift) {
14341438
cancel();
1435-
translate(shift(position, dest > prev), true);
1439+
translate(shifted, true);
14361440
}
14371441

14381442
set(MOVING);
@@ -1529,11 +1533,6 @@ function Move(Splide2, Components2, options) {
15291533
return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
15301534
}
15311535

1532-
function canShift(backwards) {
1533-
var shifted = orient(shift(getPosition(), backwards));
1534-
return backwards ? shifted >= 0 : shifted <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
1535-
}
1536-
15371536
function exceededLimit(max, position) {
15381537
position = isUndefined(position) ? getPosition() : position;
15391538
var exceededMin = max !== true && orient(position) < orient(getLimit(false));
@@ -1609,7 +1608,7 @@ function Controller(Splide2, Components2, options) {
16091608

16101609
function scroll(destination, duration, snap, callback) {
16111610
Components2.Scroll.scroll(destination, duration, snap, function () {
1612-
setIndex(loop(Move.toIndex(Move.getPosition())));
1611+
setIndex(loop(Move.toIndex(getPosition())));
16131612
callback && callback();
16141613
});
16151614
}

dist/js/splide.esm.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
44

55
/*!
66
* Splide.js
7-
* Version : 4.0.4
7+
* Version : 4.0.5
88
* License : MIT
99
* Copyright: 2022 Naotoshi Fujita
1010
*/
@@ -1423,11 +1423,15 @@ function Move(Splide2, Components2, options) {
14231423

14241424
function move(dest, index, prev, callback) {
14251425
var position = getPosition();
1426-
var crossing = sign(dest - prev) * orient(toPosition(dest) - position) < 0;
1426+
var shifted = shift(position, dest > prev);
1427+
var oriented = orient(shifted);
1428+
var destination = toPosition(dest);
1429+
var shouldShift = dest !== index || abs(shifted - destination) < abs(position - destination);
1430+
var canShift = dest > prev ? oriented >= 0 : oriented <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
14271431

1428-
if ((dest !== index || crossing) && canShift(dest > prev)) {
1432+
if (shouldShift && canShift) {
14291433
cancel();
1430-
translate(shift(position, dest > prev), true);
1434+
translate(shifted, true);
14311435
}
14321436

14331437
set(MOVING);
@@ -1524,11 +1528,6 @@ function Move(Splide2, Components2, options) {
15241528
return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
15251529
}
15261530

1527-
function canShift(backwards) {
1528-
var shifted = orient(shift(getPosition(), backwards));
1529-
return backwards ? shifted >= 0 : shifted <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
1530-
}
1531-
15321531
function exceededLimit(max, position) {
15331532
position = isUndefined(position) ? getPosition() : position;
15341533
var exceededMin = max !== true && orient(position) < orient(getLimit(false));
@@ -1604,7 +1603,7 @@ function Controller(Splide2, Components2, options) {
16041603

16051604
function scroll(destination, duration, snap, callback) {
16061605
Components2.Scroll.scroll(destination, duration, snap, function () {
1607-
setIndex(loop(Move.toIndex(Move.getPosition())));
1606+
setIndex(loop(Move.toIndex(getPosition())));
16081607
callback && callback();
16091608
});
16101609
}

dist/js/splide.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
44

55
/*!
66
* Splide.js
7-
* Version : 4.0.4
7+
* Version : 4.0.5
88
* License : MIT
99
* Copyright: 2022 Naotoshi Fujita
1010
*/
@@ -1421,11 +1421,15 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
14211421

14221422
function move(dest, index, prev, callback) {
14231423
var position = getPosition();
1424-
var crossing = sign(dest - prev) * orient(toPosition(dest) - position) < 0;
1424+
var shifted = shift(position, dest > prev);
1425+
var oriented = orient(shifted);
1426+
var destination = toPosition(dest);
1427+
var shouldShift = dest !== index || abs(shifted - destination) < abs(position - destination);
1428+
var canShift = dest > prev ? oriented >= 0 : oriented <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
14251429

1426-
if ((dest !== index || crossing) && canShift(dest > prev)) {
1430+
if (shouldShift && canShift) {
14271431
cancel();
1428-
translate(shift(position, dest > prev), true);
1432+
translate(shifted, true);
14291433
}
14301434

14311435
set(MOVING);
@@ -1522,11 +1526,6 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
15221526
return toPosition(max ? Components2.Controller.getEnd() : 0, !!options.trimSpace);
15231527
}
15241528

1525-
function canShift(backwards) {
1526-
var shifted = orient(shift(getPosition(), backwards));
1527-
return backwards ? shifted >= 0 : shifted <= list[resolve("scrollWidth")] - rect(track)[resolve("width")];
1528-
}
1529-
15301529
function exceededLimit(max, position) {
15311530
position = isUndefined(position) ? getPosition() : position;
15321531
var exceededMin = max !== true && orient(position) < orient(getLimit(false));
@@ -1602,7 +1601,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
16021601

16031602
function scroll(destination, duration, snap, callback) {
16041603
Components2.Scroll.scroll(destination, duration, snap, function () {
1605-
setIndex(loop(Move.toIndex(Move.getPosition())));
1604+
setIndex(loop(Move.toIndex(getPosition())));
16061605
callback && callback();
16071606
});
16081607
}

dist/js/splide.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/splide.min.js.gz

-4 Bytes
Binary file not shown.

dist/js/splide.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ interface ControllerComponent extends BaseComponent {
111111
scroll(destination: number, duration?: number, snap?: boolean, callback?: AnyFunction): void;
112112
getNext(destination?: boolean): number;
113113
getPrev(destination?: boolean): number;
114-
getAdjacent(prev: boolean, destination?: boolean): number;
115114
getEnd(): number;
116115
setIndex(index: number): void;
117116
getIndex(prev?: boolean): number;
@@ -120,6 +119,8 @@ interface ControllerComponent extends BaseComponent {
120119
toDest(position: number): number;
121120
hasFocus(): boolean;
122121
isBusy(): boolean;
122+
/** @internal */
123+
getAdjacent(prev: boolean, destination?: boolean): number;
123124
}
124125

125126
/**

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@splidejs/splide",
3-
"version": "4.0.4",
3+
"version": "4.0.5",
44
"description": "Splide is a lightweight, flexible and accessible slider/carousel. No dependencies, no Lighthouse errors.",
55
"author": "Naotoshi Fujita",
66
"license": "MIT",

src/js/components/Controller/Controller.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export interface ControllerComponent extends BaseComponent {
1717
scroll( destination: number, duration?: number, snap?: boolean, callback?: AnyFunction ): void;
1818
getNext( destination?: boolean ): number;
1919
getPrev( destination?: boolean ): number;
20-
getAdjacent( prev: boolean, destination?: boolean ): number;
2120
getEnd(): number;
2221
setIndex( index: number ): void;
2322
getIndex( prev?: boolean ): number;
@@ -26,6 +25,9 @@ export interface ControllerComponent extends BaseComponent {
2625
toDest( position: number ): number;
2726
hasFocus(): boolean;
2827
isBusy(): boolean;
28+
29+
/** @internal */
30+
getAdjacent( prev: boolean, destination?: boolean ): number;
2931
}
3032

3133
/**
@@ -84,7 +86,7 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
8486

8587
/**
8688
* Initializes some parameters.
87-
* Needs to check the slides length since the current index may be out of the range after refresh.
89+
* Needs to check the number of slides since the current index may be out of the range after refresh.
8890
* The process order must be Elements -> Controller -> Move.
8991
*/
9092
function init(): void {
@@ -131,7 +133,7 @@ export function Controller( Splide: Splide, Components: Components, options: Opt
131133
*/
132134
function scroll( destination: number, duration?: number, snap?: boolean, callback?: AnyFunction ): void {
133135
Components.Scroll.scroll( destination, duration, snap, () => {
134-
setIndex( loop( Move.toIndex( Move.getPosition() ) ) );
136+
setIndex( loop( Move.toIndex( getPosition() ) ) );
135137
callback && callback();
136138
} );
137139
}

0 commit comments

Comments
 (0)