Skip to content

Commit

Permalink
fix(virtual): recalc cache on removeSlide
Browse files Browse the repository at this point in the history
fixes #7020
  • Loading branch information
nolimits4web committed Sep 19, 2023
1 parent ae434b0 commit 96e5166
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/modules/virtual/virtual.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,36 @@ export default function Virtual({ swiper, extendParams, on, emit }) {
let activeIndex = swiper.activeIndex;
if (Array.isArray(slidesIndexes)) {
for (let i = slidesIndexes.length - 1; i >= 0; i -= 1) {
swiper.virtual.slides.splice(slidesIndexes[i], 1);
if (swiper.params.virtual.cache) {
delete swiper.virtual.cache[slidesIndexes[i]];
// shift cache indexes
Object.keys(swiper.virtual.cache).forEach((key) => {
if (key > slidesIndexes) {
swiper.virtual.cache[key - 1] = swiper.virtual.cache[key];
swiper.virtual.cache[key - 1].setAttribute('data-swiper-slide-index', key - 1);
delete swiper.virtual.cache[key];
}
});
}
swiper.virtual.slides.splice(slidesIndexes[i], 1);

if (slidesIndexes[i] < activeIndex) activeIndex -= 1;
activeIndex = Math.max(activeIndex, 0);
}
} else {
swiper.virtual.slides.splice(slidesIndexes, 1);
if (swiper.params.virtual.cache) {
delete swiper.virtual.cache[slidesIndexes];
// shift cache indexes
Object.keys(swiper.virtual.cache).forEach((key) => {
if (key > slidesIndexes) {
swiper.virtual.cache[key - 1] = swiper.virtual.cache[key];
swiper.virtual.cache[key - 1].setAttribute('data-swiper-slide-index', key - 1);
delete swiper.virtual.cache[key];
}
});
}
swiper.virtual.slides.splice(slidesIndexes, 1);

if (slidesIndexes < activeIndex) activeIndex -= 1;
activeIndex = Math.max(activeIndex, 0);
}
Expand Down

0 comments on commit 96e5166

Please sign in to comment.