這篇文章主要介紹swiper在vue項(xiàng)目中l(wèi)oop循環(huán)輪播失效怎么辦,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
在vue(2.5.x)中使用swiper(4.3.3),輪播加了autoplay和loop、observer、observeParents等參數(shù)還是很詭異的無(wú)法循環(huán)輪播;
那么可以這樣寫(xiě)代碼試試:
this.$api.queryImages().then((resp) => { if(resp && resp.data.resultCode == "0"){ this.swiperImgs = resp.data.data; this.$nextTick(() => { // 下一個(gè)UI幀再初始化swiper this.initSwiper(); }); } })
是的,關(guān)鍵就在于上面這個(gè)初始化swiper的調(diào)用了,一般會(huì)用v-for循環(huán)渲染,然后馬上初始化swiper,但是這樣可能導(dǎo)致初始化時(shí)v-for還沒(méi)渲染完畢,所以可能swiper錯(cuò)亂, 那么swiper放在$nextTick下一個(gè)UI幀再初始化,保證了v-for已經(jīng)完成循環(huán)。
initSwiper() { if (this.swiper != null) return; this.swiper = new Swiper('.swiper-container', { loop: true, speed: 900, autoplay: { delay: 3000, //3秒切換一次 disableOnInteraction: false }, observer: true,//修改swiper自己或子元素時(shí),自動(dòng)初始化swiper observeParents: true,//修改swiper的父元素時(shí),自動(dòng)初始化swiper pagination: { el: '.swiper-pagination', // dynamicBullets: true, } }); }
以上是“swiper在vue項(xiàng)目中l(wèi)oop循環(huán)輪播失效怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!