Skip to content

Commit

Permalink
feat(virtual): improve behavior with cssMode
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Sep 30, 2021
1 parent ef228e6 commit b478058
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/core/slide/slideTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ export default function slideTo(
const isVirtual = swiper.virtual && swiper.params.virtual.enabled;
if (isVirtual) {
swiper.wrapperEl.style.scrollSnapType = 'none';
swiper._immediateVirtual = true;
}
wrapperEl[isH ? 'scrollLeft' : 'scrollTop'] = t;
if (isVirtual) {
requestAnimationFrame(() => {
swiper.wrapperEl.style.scrollSnapType = '';
swiper._swiperImmediateVirtual = false;
});
}
} else {
Expand Down
21 changes: 18 additions & 3 deletions src/modules/virtual/virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default function Virtual({ swiper, extendParams, on }) {
},
});

let cssModeTimeout;

swiper.virtual = {
cache: {},
from: undefined,
Expand Down Expand Up @@ -49,7 +51,10 @@ export default function Virtual({ swiper, extendParams, on }) {
slidesGrid: previousSlidesGrid,
offset: previousOffset,
} = swiper.virtual;
swiper.updateActiveIndex();
if (!swiper.params.cssMode) {
swiper.updateActiveIndex();
}

const activeIndex = swiper.activeIndex || 0;

let offsetProp;
Expand Down Expand Up @@ -176,7 +181,10 @@ export default function Virtual({ swiper, extendParams, on }) {
const $cachedEl = cache[cachedIndex];
const cachedElIndex = $cachedEl.attr('data-swiper-slide-index');
if (cachedElIndex) {
$cachedEl.attr('data-swiper-slide-index', parseInt(cachedElIndex, 10) + 1);
$cachedEl.attr(
'data-swiper-slide-index',
parseInt(cachedElIndex, 10) + numberOfNewSlides,
);
}
newCache[parseInt(cachedIndex, 10) + numberOfNewSlides] = $cachedEl;
});
Expand Down Expand Up @@ -231,7 +239,14 @@ export default function Virtual({ swiper, extendParams, on }) {
});
on('setTranslate', () => {
if (!swiper.params.virtual.enabled) return;
update();
if (swiper.params.cssMode && !swiper._immediateVirtual) {
clearTimeout(cssModeTimeout);
cssModeTimeout = setTimeout(() => {
update();
}, 100);
} else {
update();
}
});
on('init update resize', () => {
if (!swiper.params.virtual.enabled) return;
Expand Down

0 comments on commit b478058

Please sign in to comment.