From 617a03741de8ae20ad0d9c34267ce5b9bc6ea2cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=A5=E8=B1=B9=E5=88=B6=E8=B1=B9?= <45782616+StarsLeopard@users.noreply.github.com> Date: Wed, 21 Aug 2024 17:21:19 +0800 Subject: [PATCH] fix(pagination) fixed swiper Infinite loop scroll jumping (#7690) * fix(pagination) Swiper Infinite loop scroll jumping * Update pagination.mjs --------- Co-authored-by: Vladimir Kharlampidi --- src/modules/pagination/pagination.mjs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/modules/pagination/pagination.mjs b/src/modules/pagination/pagination.mjs index c773b993e..fc6486c70 100644 --- a/src/modules/pagination/pagination.mjs +++ b/src/modules/pagination/pagination.mjs @@ -70,6 +70,16 @@ export default function Pagination({ swiper, extendParams, on, emit }) { } } + function getMoveDirection(prevIndex, nextIndex, length) { + prevIndex = prevIndex % length; + nextIndex = nextIndex % length; + if (nextIndex === prevIndex + 1) { + return 'next'; + } else if (nextIndex === prevIndex - 1) { + return 'previous'; + } + return; + } function onBulletClick(e) { const bulletEl = e.target.closest(classesToSelector(swiper.params.pagination.bulletClass)); if (!bulletEl) { @@ -79,7 +89,14 @@ export default function Pagination({ swiper, extendParams, on, emit }) { const index = elementIndex(bulletEl) * swiper.params.slidesPerGroup; if (swiper.params.loop) { if (swiper.realIndex === index) return; - swiper.slideToLoop(index); + const moveDirection = getMoveDirection(swiper.realIndex, index, swiper.slides.length); + if (moveDirection === 'next') { + swiper.slideNext(); + } else if (moveDirection === 'previous') { + swiper.slidePrev(); + } else { + swiper.slideToLoop(index); + } } else { swiper.slideTo(index); }