Skip to content

Commit 0771ccd

Browse files
committed
fix: use nearestAnchor in findSnappedAnchor
1 parent 8473086 commit 0771ccd

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/control/Control.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ abstract class Control {
397397
}
398398
}
399399

400-
private _getPosition(panel: Panel, direction: ValueOf<typeof DIRECTION> = DIRECTION.NONE) {
400+
protected _getPosition(panel: Panel, direction: ValueOf<typeof DIRECTION> = DIRECTION.NONE) {
401401
const flicking = getFlickingAttached(this._flicking);
402402
const camera = flicking.camera;
403403

src/control/SnapControl.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import AnchorPoint from "../core/AnchorPoint";
99
import { circulateIndex, clamp, getFlickingAttached } from "../utils";
1010
import * as AXES from "../const/axes";
1111
import * as ERROR from "../const/error";
12+
import { DIRECTION } from "../const/external";
1213

1314
import Control from "./Control";
1415

@@ -120,12 +121,16 @@ class SnapControl extends Control {
120121
});
121122
}
122123

123-
this._triggerIndexChangeEvent(targetAnchor.panel, position, axesEvent);
124+
const nextPanel = targetAnchor.panel;
125+
const direction = (posDelta === 0 || activeAnchor === targetAnchor) ? DIRECTION.NONE : (posDelta > 0 ? DIRECTION.NEXT : DIRECTION.PREV);
126+
const nextPosition = this._getPosition(nextPanel, direction);
127+
128+
this._triggerIndexChangeEvent(nextPanel, position, axesEvent);
124129

125130
return this._animateToPosition({
126-
position: camera.clampToReachablePosition(targetAnchor.position),
131+
position: camera.clampToReachablePosition(nextPosition),
127132
duration,
128-
newActivePanel: targetAnchor.panel,
133+
newActivePanel: nextPanel,
129134
axesEvent
130135
});
131136
}
@@ -138,14 +143,15 @@ class SnapControl extends Control {
138143
const currentPos = camera.position;
139144

140145
const clampedPosition = camera.clampToReachablePosition(position);
146+
const nearestAnchor = camera.findNearestAnchor(clampedPosition);
141147
const anchorAtPosition = camera.findAnchorIncludePosition(clampedPosition);
142148

143-
if (!anchorAtCamera || !anchorAtPosition) {
149+
if (!anchorAtCamera || !anchorAtPosition || !nearestAnchor) {
144150
throw new FlickingError(ERROR.MESSAGE.POSITION_NOT_REACHABLE(position), ERROR.CODE.POSITION_NOT_REACHABLE);
145151
}
146152

147153
if (!isFinite(count)) {
148-
return anchorAtPosition;
154+
return nearestAnchor;
149155
}
150156

151157
const panelCount = flicking.panelCount;

0 commit comments

Comments
 (0)