From c496835a2ce49065a9b282bbafe5629797bb3da4 Mon Sep 17 00:00:00 2001 From: Vladimir Kharlampidi Date: Wed, 6 Sep 2023 13:15:17 +0300 Subject: [PATCH] fix(core): fix loop on centeredSlides slide to beginning fixes #7011 --- src/modules/pagination/pagination.mjs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/modules/pagination/pagination.mjs b/src/modules/pagination/pagination.mjs index 1d545fd63..79a44f7bd 100644 --- a/src/modules/pagination/pagination.mjs +++ b/src/modules/pagination/pagination.mjs @@ -79,10 +79,10 @@ export default function Pagination({ swiper, extendParams, on, emit }) { const realIndex = swiper.realIndex; const newSlideIndex = swiper.getSlideIndexByData(index); const currentSlideIndex = swiper.getSlideIndexByData(swiper.realIndex); - if (newSlideIndex > swiper.slides.length - swiper.loopedSlides) { + const loopFix = (dir) => { const indexBeforeLoopFix = swiper.activeIndex; swiper.loopFix({ - direction: newSlideIndex > currentSlideIndex ? 'next' : 'prev', + direction: dir, activeSlideIndex: newSlideIndex, slideTo: false, }); @@ -90,6 +90,17 @@ export default function Pagination({ swiper, extendParams, on, emit }) { if (indexBeforeLoopFix === indexAfterFix) { swiper.slideToLoop(realIndex, 0, false, true); } + }; + if (newSlideIndex > swiper.slides.length - swiper.loopedSlides) { + loopFix(newSlideIndex > currentSlideIndex ? 'next' : 'prev'); + } else if (swiper.params.centeredSlides) { + const slidesPerView = + swiper.params.slidesPerView === 'auto' + ? swiper.slidesPerViewDynamic() + : Math.ceil(parseFloat(swiper.params.slidesPerView, 10)); + if (newSlideIndex < Math.floor(slidesPerView / 2)) { + loopFix('prev'); + } } swiper.slideToLoop(index);