I'm using Swiper.js.
What I'm trying to achieve:
I have multiple swipers and each has its own 'Reset' button.
By clicking the 'Reset' button I want the swiper to go to its first slide and reset/refresh the whole swiper.
The problem:
A single swiper in the website does what I want.
Once I add multiple swiper containers the 'Reset' button stops working.
JSFiddle
Add multiple swiper containers to see what I'm facing.
https://jsfiddle.net/alihoo/g3nfea5c/4/
HTML
CSS
.swiper-container {
width: 100%;
height: 364px;
}
.swiper-slide {
line-height: 300px;
width: 300px;
background: #222;
color: white;
font-size: 20px;
text-align: center;
}
button {
font-size: 16px;
width: 100%;
height: 32px;
margin-bottom: 32px;
background: #007AFF;
border: none;
color: white;
border-radius: 10px;
cursor: pointer;
}
.swiper-pagination {
color: #007AFF;
}
JS
var settings = {
slidesPerView: 'auto',
spaceBetween: 32,
initialSlide: 0,
loop: true,
loopAdditionalSlides: 1,
freeMode: true,
freeModeMinimumVelocity: 0.5,
freeModeMomentumBounce: false,
grabCursor: true,
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
pagination: {
el: '.swiper-pagination',
type: 'custom',
renderCustom: function(swiper, current, total) {
return ('0' + current).slice(-2) + '/' + ('0' + total).slice(-2);
}
}
};
var mySwiper = new Swiper('.swiper-container', settings);
function resetSwiper() {
var swiperWrapper = $('.swiper-wrapper');
var newSlides = $('.swiper-wrapper').children('.swiper-slide').clone(true);
mySwiper.destroy();
swiperWrapper.empty().append(newSlides);
$('.swiper-wrapper').attr('style', '');
mySwiper = new Swiper('.swiper-container', settings);
}
$('.reset').click(function() {
resetSwiper();
});