您好,登錄后才能下訂單哦!
這篇文章主要介紹了Vue怎么使用Swiper的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Vue怎么使用Swiper文章都會有所收獲,下面我們一起來看看吧。
npm i swiper@5.2.0
<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.name}}</div> </div> <!-- 分頁器 --> <div class="swiper-pagination"></div> <!--導航器--> <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數據發生變化時,更新swiper slideList:{ deep:true, // eslint-disable-next-line handler(nv,ov){ console.log("數據更新了") this.updateSwiper() } } }, mounted() { this.initSwiper() }, methods:{ //鼠標移入暫停自動播放 stopAutoPlay() { this.currentSwiper.autoplay.stop() }, //鼠標移出開始自動播放 startAutoPlay() { this.currentSwiper.autoplay.start() }, //初始化swiper initSwiper(){ // eslint-disable-next-line let vueComponent=this//獲取vue組件實例 //一個頁面有多個swiper實例時,為了不互相影響,綁定容器用不同值或變量綁定 this.currentSwiper = new Swiper('#'+this.currentIndex, { loop: true, // 循環模式選項 autoHeight:'true',//開啟自適應高度,容器高度由slide高度決定 //分頁器 pagination: { el: '.swiper-pagination', clickable:true,//分頁器按鈕可點擊 }, on: { //此處this為swiper實例 //切換結束獲取slide真實下標 slideChangeTransitionEnd: function(){ console.log(vueComponent.$props.currentIndex+"號swiper實例真實下標",this.realIndex) }, //綁定點擊事件,解決loop:true時事件丟失 // eslint-disable-next-line click: function(event){ console.log("你點擊了"+vueComponent.$props.currentIndex+"號swiper組件") } }, //導航器 navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, autoplay: { //自動播放,不同版本配置方式不同 delay: 3000, stopOnLastSlide: false, disableOnInteraction: false }, slidesPerView: 1, //視口展示slide數1 slidesPerGroup: 1, //slide數1頁一組 }) }, //銷毀swiper destroySwiper(){ try { this.currentSwiper.destroy(true,false) }catch (e) { console.log("刪除輪播") } }, //更新swiper updateSwiper(){ this.destroySwiper() this.$nextTick(()=>{ this.initSwiper() }) }, }, destroyed() { this.destroySwiper() } } </script> <style scoped> .CarouselContainer{ background-color: gray; } /*slide樣式*/ .my-swiper-slide{ height: 300px; background-color: pink; } /*swiper容器樣式*/ .swiper-container { width: 700px; border: 1px solid red; } /*自定義分頁器按鈕被點擊選中時的樣式*/ /deep/.swiper-pagination-bullet-active{ background-color: #d5a72f !important; width: 20px; } /*自定義分頁器按鈕常規樣式*/ /deep/.swiper-pagination-bullet{ background-color: #9624bf; opacity: 1; width: 20px; } </style>
<template> <div id="app"> <!--傳遞不同的currentIndex 作為區分不同swiper組件的動態id--> <CarouselContainer :slide-list="list" currentIndex="1"></CarouselContainer> <CarouselContainer :slide-list="list" currentIndex="2"></CarouselContainer> </div> </template> <script> import CarouselContainer from './components/CarouselContainer.vue' export default { name: 'App', components: { CarouselContainer, }, data(){ return{ list:[ {name:"aaa"}, {name:"bbb"}, {name:"ccc"}, ] } }, mounted() { let self=this //延時模擬兩次數據變化 setTimeout(()=>{ let obj={name:"ddd"} self.list.push(obj) },5000) setTimeout(()=>{ let obj={name:"eee"} self.list[0].name="AAA" self.list.push(obj) },8000) } } </script> <style scoped> </style>
只需要這兩個文件就可以vue項目中運行demo查看效果了
關于“Vue怎么使用Swiper”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Vue怎么使用Swiper”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。