Skip to content

Commit

Permalink
chore: more Virtual loop
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Dec 6, 2022
1 parent c7d1a50 commit d23e809
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
12 changes: 11 additions & 1 deletion src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,17 @@ class Swiper {
}

// Slide To Initial Slide
swiper.slideTo(swiper.params.initialSlide, 0, swiper.params.runCallbacksOnInit, false, true);
if (swiper.params.loop && swiper.virtual && swiper.params.virtual.enabled) {
swiper.slideTo(
swiper.params.initialSlide + swiper.virtual.slidesBefore,
0,
swiper.params.runCallbacksOnInit,
false,
true,
);
} else {
swiper.slideTo(swiper.params.initialSlide, 0, swiper.params.runCallbacksOnInit, false, true);
}

// Create loop
if (swiper.params.loop) {
Expand Down
4 changes: 3 additions & 1 deletion src/core/events/onResize.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default function onResize() {
// Save locks
const { allowSlideNext, allowSlidePrev, snapGrid } = swiper;

const isVirtual = swiper.virtual && swiper.params.virtual.enabled;

// Disable locks on resize
swiper.allowSlideNext = true;
swiper.allowSlidePrev = true;
Expand All @@ -30,7 +32,7 @@ export default function onResize() {
) {
swiper.slideTo(swiper.slides.length - 1, 0, false, true);
} else {
if (swiper.params.loop) {
if (swiper.params.loop && !isVirtual) {
swiper.slideToLoop(swiper.realIndex, 0, false, true);
} else {
swiper.slideTo(swiper.activeIndex, 0, false, true);
Expand Down
2 changes: 1 addition & 1 deletion src/core/loop/loopFix.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function loopFix(slideRealIndex, slideTo = true) {
if (swiper.snapIndex === 0) {
swiper.slideTo(swiper.virtual.slides.length, 0, false, true);
} else if (swiper.snapIndex === swiper.snapGrid.length - 1) {
swiper.slideTo(0, 0, false, true);
swiper.slideTo(swiper.virtual.slidesBefore, 0, false, true);
}
}
swiper.allowSlidePrev = allowSlidePrev;
Expand Down
29 changes: 18 additions & 11 deletions src/core/update/updateActiveIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export default function updateActiveIndex(newActiveIndex) {
} = swiper;
let activeIndex = newActiveIndex;
let snapIndex;

const getVirtualRealIndex = (aIndex) => {
let realIndex = aIndex - swiper.virtual.slidesBefore;
if (realIndex < 0) {
realIndex = swiper.virtual.slides.length - 1;
}
return realIndex;
};
if (typeof activeIndex === 'undefined') {
activeIndex = getActiveIndexByTranslate(swiper);
}
Expand All @@ -50,21 +58,20 @@ export default function updateActiveIndex(newActiveIndex) {
swiper.emit('snapIndexChange');
}
if (swiper.params.loop && swiper.virtual && swiper.params.virtual.enabled) {
const realIndex = parseInt(
swiper.slides.eq(activeIndex).attr('data-swiper-slide-index') || activeIndex,
10,
);
Object.assign(swiper, {
realIndex,
});
swiper.realIndex = getVirtualRealIndex(activeIndex);
}
return;
}
// Get real index
const realIndex = parseInt(
swiper.slides.eq(activeIndex).attr('data-swiper-slide-index') || activeIndex,
10,
);
let realIndex;
if (swiper.virtual && params.virtual.enabled && params.loop) {
realIndex = getVirtualRealIndex(activeIndex);
} else {
realIndex = parseInt(
swiper.slides.eq(activeIndex).attr('data-swiper-slide-index') || activeIndex,
10,
);
}

Object.assign(swiper, {
snapIndex,
Expand Down
6 changes: 5 additions & 1 deletion src/core/update/updateSlides.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ export default function updateSlides() {
}
}
if (isVirtual && params.loop) {
snapGrid.push(snapGrid[snapGrid.length - 1] + swiperSize);
snapGrid.push(
snapGrid[snapGrid.length - 1] + slidesSizesGrid[0] + spaceBetween,
snapGrid[snapGrid.length - 1] + (slidesSizesGrid[0] + spaceBetween) * 2,
);
slidesGrid.push(slidesGrid[slidesGrid.length - 1] + slidesSizesGrid[0] + spaceBetween);
}
if (snapGrid.length === 0) snapGrid = [0];

Expand Down
8 changes: 7 additions & 1 deletion src/core/update/updateSlidesClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ export default function updateSlidesClasses() {

let activeSlide;
if (isVirtual) {
activeSlide = getFilteredSlides(`[data-swiper-slide-index="${activeIndex}"]`);
if (params.loop) {
activeSlide = getFilteredSlides(
`[data-swiper-slide-index="${activeIndex - swiper.virtual.slidesBefore}"]`,
);
} else {
activeSlide = getFilteredSlides(`[data-swiper-slide-index="${activeIndex}"]`);
}
} else {
activeSlide = slides.eq(activeIndex);
}
Expand Down

0 comments on commit d23e809

Please sign in to comment.