更改幻灯片时如何获取当前幻灯片的索引?
How can I get the index of the current slide, when the slide is changed?
我有以下
mySwiper = new Swiper('.my-swiper', {
direction: 'vertical',
loop: true,
});
mySwiper.on('slideChange', function () {
console.log('*** mySwiper.realIndex', mySwiper.realIndex);
});
但是 realIndex
总是 returns 上一个索引。因此,如果我在第一张幻灯片 (0) 上转到下一张幻灯片,realIndex
应该为 1 时为 0。当我返回第一张幻灯片时,realIndex
应该为 1 0. 因此事件似乎在索引更新之前触发。我尝试过其他活动,但没有任何运气。幻灯片更改后是否有事件触发,以便我可以捕获当前幻灯片索引?
根据 API,有一个事件 slideChangeTransitionEnd
“...将在另一张幻灯片(下一张或上一张)的动画之后触发”。这听起来像是一个很有前途的事件,可以尝试而不是 slideChange
。
您应该使用 transitionEnd
事件,因为事件将在转换后触发。
查看下面的演示
var mySwiper = new Swiper('.swiper-container', {
direction: 'vertical',
loop: true,
});
mySwiper.on('transitionEnd', function() {
console.log('*** mySwiper.realIndex', mySwiper.realIndex);
});
.swiper-container {
width: 100%;
height: 100%;
}
.as-console-wrapper {
z-index: 999999 !important;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
<!-- Link Swiper's CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.2.6/css/swiper.min.css">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</div>
</div>
<!-- Swiper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.2.6/js/swiper.min.js"></script>
注意:如果您无法在片段中看到 console.log
语句输出,请使用 F12
查看幻灯片的索引。
mySwiper.on('slideChange', function (e) {
console.log('*** mySwiper.activeIndex', mySwiper.activeIndex);
});
您可以在回调函数中访问所有 属性 滑动器可用的方法。
var swiper = new Swiper(".mySwiper", {
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
}
});
swiper.on('slideChange', function () {
myCallbackfunction(this);
});
function myCallbackfunction(data){
console.log(data.realIndex);
//console.log(data);
}
我有以下
mySwiper = new Swiper('.my-swiper', {
direction: 'vertical',
loop: true,
});
mySwiper.on('slideChange', function () {
console.log('*** mySwiper.realIndex', mySwiper.realIndex);
});
但是 realIndex
总是 returns 上一个索引。因此,如果我在第一张幻灯片 (0) 上转到下一张幻灯片,realIndex
应该为 1 时为 0。当我返回第一张幻灯片时,realIndex
应该为 1 0. 因此事件似乎在索引更新之前触发。我尝试过其他活动,但没有任何运气。幻灯片更改后是否有事件触发,以便我可以捕获当前幻灯片索引?
根据 API,有一个事件 slideChangeTransitionEnd
“...将在另一张幻灯片(下一张或上一张)的动画之后触发”。这听起来像是一个很有前途的事件,可以尝试而不是 slideChange
。
您应该使用 transitionEnd
事件,因为事件将在转换后触发。
查看下面的演示
var mySwiper = new Swiper('.swiper-container', {
direction: 'vertical',
loop: true,
});
mySwiper.on('transitionEnd', function() {
console.log('*** mySwiper.realIndex', mySwiper.realIndex);
});
.swiper-container {
width: 100%;
height: 100%;
}
.as-console-wrapper {
z-index: 999999 !important;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
<!-- Link Swiper's CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.2.6/css/swiper.min.css">
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</div>
</div>
<!-- Swiper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.2.6/js/swiper.min.js"></script>
注意:如果您无法在片段中看到 console.log
语句输出,请使用 F12
查看幻灯片的索引。
mySwiper.on('slideChange', function (e) {
console.log('*** mySwiper.activeIndex', mySwiper.activeIndex);
});
您可以在回调函数中访问所有 属性 滑动器可用的方法。
var swiper = new Swiper(".mySwiper", {
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
}
});
swiper.on('slideChange', function () {
myCallbackfunction(this);
});
function myCallbackfunction(data){
console.log(data.realIndex);
//console.log(data);
}