Vue使用swiper問題(5.2.0版本,避免踩坑)
Vue使用Swiper看這一篇就夠了
如果你還在用swiper@5.0以下的版本,如果你還在為坑多解決不了而煩惱,(ps:我已經(jīng)踩了好多天的坑了)那么你不妨靜下心來(lái)看完這篇博客,相信你會(huì)選擇5.0版本的!?。?/p>
滿足以下需求:
- 完成swiper動(dòng)態(tài)異步數(shù)據(jù)下的slide重新渲染
- 解決loop:true設(shè)置時(shí)的事件丟失問題
- swiper鼠標(biāo)移入/移出 暫停/開始輪播
- 單頁(yè)面渲染多個(gè)swiper組件互不影響
- slide只有一頁(yè)時(shí)停止自動(dòng)滾動(dòng)
- 自定義配置選項(xiàng)
一、下載指定版本swiper
npm i swiper@5.2.0
二、創(chuàng)建輪播圖組件CarouselContainer.vue
詳細(xì)解析在代碼注釋中
<template> <div class="CarouselContainer" @mouseenter="stopAutoPlay" @mouseleave="startAutoPlay" > <div ref="mySwiper" class="swiper-container" :id="currentIndex"> <div class="swiper-wrapper"> <div class="swiper-slide my-swiper-slide" v-for="(item, index) of slideList" :key="index" > {{ item }} </div> </div> <!-- 分頁(yè)器 --> <!-- <div class="swiper-pagination"></div> --> <!--導(dǎo)航器--> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> </div> </div> </template> <script> import Swiper from "swiper"; import "swiper/css/swiper.css"; export default { name: "CarouselContainer", props: ["slideList", "currentIndex"], data() { return { currentSwiper: null, }; }, watch: { //slide數(shù)據(jù)發(fā)生變化時(shí),更新swiper slideList: { deep: true, // eslint-disable-next-line handler(nv, ov) { console.log("數(shù)據(jù)更新了"); this.updateSwiper(); }, }, }, mounted() { this.initSwiper(); }, methods: { //鼠標(biāo)移入暫停自動(dòng)播放 stopAutoPlay() { this.currentSwiper.autoplay.stop(); }, //鼠標(biāo)移出開始自動(dòng)播放 startAutoPlay() { this.currentSwiper.autoplay.start(); }, //初始化swiper initSwiper() { // eslint-disable-next-line let vueComponent = this; //獲取vue組件實(shí)例 //一個(gè)頁(yè)面有多個(gè)swiper實(shí)例時(shí),為了不互相影響,綁定容器用不同值或變量綁定 this.currentSwiper = new Swiper("#" + this.currentIndex, { // 循環(huán)模式選項(xiàng)loop,默認(rèn)為false,可動(dòng)態(tài)設(shè)置,當(dāng)slide只有一頁(yè)時(shí)置為false,>1頁(yè)時(shí)置為true loop: true, // 循環(huán)模式選項(xiàng) autoHeight: "true", //開啟自適應(yīng)高度,容器高度由slide高度決定 //分頁(yè)器 // pagination: { // el: '.swiper-pagination', // clickable:true,//分頁(yè)器按鈕可點(diǎn)擊 // }, //grabCursor: true, //小手掌抓取滑動(dòng) // direction: "vertical", // 縱向滾動(dòng),默認(rèn)是橫向滾動(dòng)的 on: { //此處this為swiper實(shí)例 //切換結(jié)束獲取slide真實(shí)下標(biāo) slideChangeTransitionEnd: function () { console.log(vueComponent.$props.currentIndex+"號(hào)swiper實(shí)例真實(shí)下標(biāo)",this.realIndex) }, //綁定點(diǎn)擊事件,解決loop:true時(shí)事件丟失 // eslint-disable-next-line click: function (event) { console.log("你點(diǎn)擊了"+vueComponent.$props.currentIndex+"號(hào)swiper組件") }, }, //導(dǎo)航器 navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev", }, autoplay: { //自動(dòng)播放,不同版本配置方式不同 delay: 2000, stopOnLastSlide: false, disableOnInteraction: false, // 用戶操作之后是否停止自動(dòng)輪播默認(rèn)true }, slidesPerView: 1, //視口展示slide數(shù)1 slidesPerGroup: 1, //slide數(shù)1頁(yè)一組 }); }, //銷毀swiper destroySwiper() { try { // 此處destroy(bool1,bool2) // bool1代表是否銷毀swiper實(shí)例 // bool2代表是否銷毀swiper樣式(導(dǎo)航器、分頁(yè)器等) this.currentSwiper.destroy(true, false); } catch (e) { console.log("刪除輪播"); } }, //更新swiper(先銷毀 再 重新初始化swiper) updateSwiper() { this.destroySwiper(); this.$nextTick(() => { this.initSwiper(); }); }, }, // 組件銷毀之前 銷毀 swiper 實(shí)例 beforeDestroy() { this.destroySwiper(); }, }; </script> <style scoped lang="scss"> .CarouselContainer { width: 100%; height: 100%; background-color: gray; } /*slide樣式*/ .my-swiper-slide { height: 300px; background-color: pink; } /*swiper容器樣式*/ .swiper-container { width: 700px; border: 1px solid red; } // /*自定義分頁(yè)器按鈕被點(diǎn)擊選中時(shí)的樣式*/ // ::v-deep(.swiper-pagination-bullet-active){ // background-color: #d5a72f !important; // width: 20px; // } // /*自定義分頁(yè)器按鈕常規(guī)樣式*/ // ::v-deep(.swiper-pagination-bullet){ // background-color: #9624bf; // opacity: 1; // width: 20px; // } </style>
三、創(chuàng)建父組件Father.vue渲染多個(gè)swiper組件、模擬異步數(shù)據(jù)變化
<template> <div class="father"> <!--傳遞不同的currentIndex 作為區(qū)分不同swiper組件的動(dòng)態(tài)id--> <CarouselContainer :slide-list="list" currentIndex="1"></CarouselContainer> <CarouselContainer :slide-list="list" currentIndex="2"></CarouselContainer> <button @click="changeData">更換數(shù)據(jù)嘍</button> </div> </template> <script> import CarouselContainer from './components/CarouselContainer.vue' export default { components: { CarouselContainer, }, data(){ return{ list:['a','b','c'] } }, methods: { changeData(){ const swiperList = ['我是圖片1','我是圖片2','我是圖片3']; this.list = swiperList ; } } } </script> <style scoped> </style>
完成之后就可以在你的項(xiàng)目中看到效果啦,之后可以根據(jù)項(xiàng)目需求去改進(jìn)…
修改
此處追加說明destroy()釋放swiper實(shí)例的幾種情況:
解決問題:內(nèi)存增長(zhǎng)
此處destroy(bool1,bool2)
- bool1代表是否銷毀swiper實(shí)例
- bool2代表是否銷毀swiper樣式(導(dǎo)航器、分頁(yè)器等)
情景一:如果只更新swiper里面的數(shù)據(jù),destroy(true,false)
情景二:如果要銷毀(跳轉(zhuǎn)路由銷毀組件,遍歷重新new一個(gè)swiper實(shí)例)swiper實(shí)例,destroy(true,true)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解
這篇文章主要為大家介紹了vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09在vue項(xiàng)目中配置你自己的啟動(dòng)命令和打包命令方式
這篇文章主要介紹了在vue項(xiàng)目中配置你自己的啟動(dòng)命令和打包命令方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue元素實(shí)現(xiàn)動(dòng)畫過渡效果
這篇文章主要介紹了vue元素實(shí)現(xiàn)動(dòng)畫過渡效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-07-07解決Element ui導(dǎo)航欄選中背景色刷新消失的問題
這篇文章主要介紹了解決Element ui導(dǎo)航欄選中背景色刷新消失的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05詳解VS Code使用之Vue工程配置format代碼格式化
這篇文章主要介紹了詳解VS Code使用之Vue工程配置format代碼格式化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Vue3.0導(dǎo)出數(shù)據(jù)為自定義樣式Excel的詳細(xì)實(shí)例
在許多的后臺(tái)系統(tǒng)中少不了導(dǎo)出Excel表格的功能,下面這篇文章主要給大家介紹了關(guān)于Vue3.0導(dǎo)出數(shù)據(jù)為自定義樣式Excel的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06