1. 程式人生 > 實用技巧 >swiper使用loop後,無法獲取索引的問題

swiper使用loop後,無法獲取索引的問題

swiper官網中(https://www.swiper.com.cn/api/properties/310.html)提到

就是說如果寫了 loop:true是的時候,會在迴圈完切換到第一張,但是獲取索引就是realIndex

下面上程式碼

 <div class="main-left">
                    <div class="swiper-container swiper-banner">
                        <div class="swiper-wrapper">
                            <
div class="swiper-slide slide-items"> <a class="slide-a"> <img src="http://www.chooseauto.com.cn/image2/2019/11/23/CCD7D1D053B645F7B8C11BC0F75116FA.jpg"> </
a> <div class="slide-tit"> <a href="/zixun/hangye/307046.shtml" target="_blank">新“雙積分”政策指向何方</a> </div> </div> <
div class="swiper-slide slide-items"> <a class="slide-a"> <img src="http://www.chooseauto.com.cn/image3/2020/7/6/25FE83B2DF3549A99C9D57A48D8E6236.png"> </a> <div class="slide-tit"> <a href="/zixun/hangye/307046.shtml" target="_blank">新“雙積分”政策指向何方</a> </div> </div> <div class="swiper-slide slide-items"> <a class="slide-a"> <img src="http://www.chooseauto.com.cn/image4/2020/7/7/C369CDF2C4C14E4EA1146F872138F999.jpg"> </a> <div class="slide-tit"> <a href="/zixun/hangye/307046.shtml" target="_blank">新“雙積分”政策指向何方</a> </div> </div> </div> <div class="swiper-button-prev arrow-left" ></div> <!--左箭頭。如果放置在swiper-container外面,需要自定義樣式。--> <div class="swiper-button-next arrow-right"></div> <!--右箭頭。如果放置在swiper-container外面,需要自定義樣式。--> </div> <div class="index-wrap"> <span class="active-num">1</span></div> </div>

js

var mySwiper = new Swiper('.swiper-banner', {
    autoplay: true,//自動播放
    loop: true,//迴圈播放
    pagination: {
        el: '#banner_paination',
    },
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },
    on:{
        slideChangeTransitionEnd:function(){
//獲取當前的索引 $(
".active-num").html(this.realIndex + 1); } } })

效果圖