根据这里作为答案提供的指示......
iDangerous Swiper plugin reset slides
我试图做这样的事情:
http://jsfiddle.net/monastic/ydKn2/17/
<div id="slide-repo">
<div class="swiper-slide">
<img src="http://dummyimage.com/100x100/000/fff.jpg" />
</div>
...................
</div>
<div>
<button id="update-slides">Update Slides</button>
</div>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<img src="http://dummyimage.com/100x100/ff0000/fff.jpg" />
</div>
.................
</div>
</div>
var mySwiper = new Swiper('.swiper-container', {
mode: 'vertical',
loop: true,
loopAdditionalSlides: 5,
centeredSlides: true,
slidesPerView: 2,
initialSlide: 0,
});
$('button#update-slides').on('click', function(){
var swiperWrapper = $('.swiper-wrapper'),
newSlides = $('#slide-repo').children('.swiper-slide').clone(true);
mySwiper.destroy();
swiperWrapper.empty().append(newSlides);
$('.swiper-wrapper').attr('style', '');
mySwiper.reInit();
});
但是控制台正在返回&#39;无法读取属性&#39; init&#39; null&#39;。
有什么建议吗?
答案 0 :(得分:5)
我认为这是因为您正在调用mySwiper.destroy()
,因此无法运行reInit()
。文档说reInit用于在添加或删除幻灯片时重置。但是在这里,你在一个不再是傻瓜的元素上调用reInit。
相反,您可以每次重新创建傻瓜。 (我不确定为什么只调用removeAllSlides
,然后reInit
不会保留相同的设置。)
var settings = {
mode: 'vertical',
loop: true,
loopAdditionalSlides: 5,
centeredSlides: true,
slidesPerView: 2,
initialSlide: 0,
},
mySwiper = new Swiper('.swiper-container', settings);
$('button#update-slides').on('click', function(){
var swiperWrapper = $('.swiper-wrapper'),
newSlides = $('#slide-repo').children('.swiper-slide').clone(true);
mySwiper.destroy();
swiperWrapper.empty().append(newSlides);
$('.swiper-wrapper').attr('style', '');
mySwiper = new Swiper('.swiper-container', settings);
});