// this.activeIndex 切换到当前的索引值
new Swiper('.swiper', {
loop: data.length > 1 ? true : false,
pagination: { // 分页配置函数
el: '.swiper-pagination',
},
autoplay: { // 自动切换配置函数
delay: 3000,
stopOnLastSlide: false,
disableOnInteraction: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
on:{
// 以下函数的逻辑获取第几张是在循环模式下,loop:true,非循环模式下第几张直接是 this.activeIndex+1(索引+1)
slideChangeTransitionStart: function () { // 切换动作开始函数
let ind = 1; // 同按钮同步当前切换是第几个变量
// star
// data 图片数组
if(this.activeIndex == 0){
ind = data.length;
} else if(this.activeIndex == data.length+1){
ind = 1;
} else {
ind = this.activeIndex;
}
// end
$('#img-ind').text(ind); // 最终获取到的同步值输出到页面
}
},
slideChangeTransitionEnd: function () { // 切换动作结束函数
},
slideChange: function () { // swiper切换了干某些事的函数
// do somethings
},
})
swiper 不停销毁重建场景
重建之前最好先销毁
var swiper = null;
swiper && swiper.destroy && swiper.destroy(false, true)
swiper = new Swiper('.swiper')
以上个别方法支持版本>=4.0.0
小于4.0.0,兼容IE10写法:
new Swiper('.swiper', {
loop: data.length > 1 ? true : false,
pagination: '.swiper-pagination',
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',
onTransitionStart: function (ele) {
let ind = 1;
if(ele.activeIndex == 0){
ind = data.length;
} else if(ele.activeIndex == data.length+1){
ind = 1;
} else {
ind = ele.activeIndex;
}
$('#img-ind').text(ind);
},
onTransitionEnd: function (ele) { // 切换动作结束函数
},
onChange: function (ele) { // swiper切换了干某些事的函数
// do somethings
},
})