Skip to content

Commit

Permalink
Virtual: removeSlide, removeAllSlides + tweaks for preprendSlides
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Feb 22, 2019
1 parent 353a910 commit 4afc28f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
10 changes: 5 additions & 5 deletions demos/410-virtual-slides.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

<!-- Initialize Swiper -->
<script>
var appendNumber = 1;
var appendNumber = 600;
var prependNumber = 1;
var swiper = new Swiper('.swiper-container', {
slidesPerView: 3,
Expand Down Expand Up @@ -119,13 +119,13 @@
document.querySelector('.prepend-2-slides').addEventListener('click', function (e) {
e.preventDefault();
swiper.virtual.prependSlide([
'<div class="swiper-slide">Slide ' + (--prependNumber) + '</div>',
'<div class="swiper-slide">Slide ' + (--prependNumber) + '</div>'
]);
'Slide ' + (--prependNumber),
'Slide ' + (--prependNumber)
]);
});
document.querySelector('.append-slide').addEventListener('click', function (e) {
e.preventDefault();
swiper.virtual.appendSlide('<div class="swiper-slide">Slide ' + (++appendNumber) + '</div>');
swiper.virtual.appendSlide('Slide ' + (++appendNumber));
});
</script>
</body>
Expand Down
39 changes: 37 additions & 2 deletions src/components/virtual/virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const Virtual = {
let newActiveIndex = activeIndex + 1;
let numberOfNewSlides = 1;

if (typeof slides === 'object' && 'length' in slides) {
if (Array.isArray(slides)) {
for (let i = 0; i < slides.length; i += 1) {
if (slides[i]) swiper.virtual.slides.unshift(slides[i]);
}
Expand All @@ -146,13 +146,46 @@ const Virtual = {
const cache = swiper.virtual.cache;
const newCache = {};
Object.keys(cache).forEach((cachedIndex) => {
newCache[cachedIndex + numberOfNewSlides] = cache[cachedIndex];
newCache[parseInt(cachedIndex, 10) + numberOfNewSlides] = cache[cachedIndex];
});
swiper.virtual.cache = newCache;
}
swiper.virtual.update(true);
swiper.slideTo(newActiveIndex, 0);
},
removeSlide(slidesIndexes) {
const swiper = this;
if (typeof slidesIndexes === 'undefined' || slidesIndexes === null) return;
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]];
}
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];
}
if (slidesIndexes < activeIndex) activeIndex -= 1;
activeIndex = Math.max(activeIndex, 0);
}
swiper.virtual.update(true);
swiper.slideTo(activeIndex, 0);
},
removeAllSlides() {
const swiper = this;
swiper.virtual.slides = [];
if (swiper.params.virtual.cache) {
swiper.virtual.cache = {};
}
swiper.virtual.update(true);
swiper.slideTo(0, 0);
},
};

export default {
Expand All @@ -175,6 +208,8 @@ export default {
update: Virtual.update.bind(swiper),
appendSlide: Virtual.appendSlide.bind(swiper),
prependSlide: Virtual.prependSlide.bind(swiper),
removeSlide: Virtual.removeSlide.bind(swiper),
removeAllSlides: Virtual.removeAllSlides.bind(swiper),
renderSlide: Virtual.renderSlide.bind(swiper),
slides: swiper.params.virtual.slides,
cache: {},
Expand Down

0 comments on commit 4afc28f

Please sign in to comment.