Skip to content

Commit

Permalink
fix(core): fixed loop behavior in free mode with mousewheel
Browse files Browse the repository at this point in the history
fixes #6323
  • Loading branch information
nolimits4web committed Feb 2, 2023
1 parent 2915de7 commit 336d908
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
23 changes: 15 additions & 8 deletions src/core/loop/loopFix.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function loopFix({
setTranslate,
activeSlideIndex,
byController,
byMousewheel,
} = {}) {
const swiper = this;
if (!swiper.params.loop) return;
Expand Down Expand Up @@ -97,10 +98,13 @@ export default function loopFix({
const currentSlideTranslate = swiper.slidesGrid[activeIndex];
const newSlideTranslate = swiper.slidesGrid[activeIndex + slidesPrepended];
const diff = newSlideTranslate - currentSlideTranslate;

swiper.slideTo(activeIndex + slidesPrepended, 0, false, true);
if (setTranslate) {
swiper.touches[swiper.isHorizontal() ? 'startX' : 'startY'] += diff;
if (byMousewheel) {
swiper.setTranslate(swiper.translate - diff);
} else {
swiper.slideTo(activeIndex + slidesPrepended, 0, false, true);
if (setTranslate) {
swiper.touches[swiper.isHorizontal() ? 'startX' : 'startY'] += diff;
}
}
} else {
if (setTranslate) {
Expand All @@ -112,10 +116,13 @@ export default function loopFix({
const currentSlideTranslate = swiper.slidesGrid[activeIndex];
const newSlideTranslate = swiper.slidesGrid[activeIndex - slidesAppended];
const diff = newSlideTranslate - currentSlideTranslate;

swiper.slideTo(activeIndex - slidesAppended, 0, false, true);
if (setTranslate) {
swiper.touches[swiper.isHorizontal() ? 'startX' : 'startY'] += diff;
if (byMousewheel) {
swiper.setTranslate(swiper.translate - diff);
} else {
swiper.slideTo(activeIndex - slidesAppended, 0, false, true);
if (setTranslate) {
swiper.touches[swiper.isHorizontal() ? 'startX' : 'startY'] += diff;
}
}
} else {
swiper.slideToLoop(slideRealIndex, 0, false, true);
Expand Down
10 changes: 7 additions & 3 deletions src/modules/mousewheel/mousewheel.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export default function Mousewheel({ swiper, extendParams, on, emit }) {
delta: Math.abs(delta),
direction: Math.sign(delta),
};

const ignoreWheelEvents =
lastEventBeforeSnap &&
newEvent.time < lastEventBeforeSnap.time + 500 &&
Expand All @@ -290,9 +291,6 @@ export default function Mousewheel({ swiper, extendParams, on, emit }) {
if (!ignoreWheelEvents) {
lastEventBeforeSnap = undefined;

if (swiper.params.loop) {
swiper.loopFix();
}
let position = swiper.getTranslate() + delta * params.sensitivity;
const wasBeginning = swiper.isBeginning;
const wasEnd = swiper.isEnd;
Expand All @@ -309,6 +307,12 @@ export default function Mousewheel({ swiper, extendParams, on, emit }) {
if ((!wasBeginning && swiper.isBeginning) || (!wasEnd && swiper.isEnd)) {
swiper.updateSlidesClasses();
}
if (swiper.params.loop) {
swiper.loopFix({
direction: newEvent.direction < 0 ? 'next' : 'prev',
byMousewheel: true,
});
}

if (swiper.params.freeMode.sticky) {
// When wheel scrolling starts with sticky (aka snap) enabled, then detect
Expand Down

0 comments on commit 336d908

Please sign in to comment.