欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue使用Swiper的案例詳解

 更新時(shí)間:2022年06月22日 12:01:24   作者:Tyler Yue  
這篇文章主要介紹了Vue使用Swiper的案例詳解,主要包括引入swiper,創(chuàng)建輪播圖組件CarouselContainer.vue的詳細(xì)代碼,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下

Vue使用Swiper看這一篇就夠了

此案例實(shí)現(xiàn)需求

  • 完成swiper動(dòng)態(tài)異步數(shù)據(jù)下的slide渲染
  • 自定義分頁(yè)器樣式
  • 解決loop:true設(shè)置時(shí)的事件丟失問(wèn)題
  • swiper鼠標(biāo)移入/移出 暫停/開始輪播
  • 單頁(yè)面渲染多個(gè)swiper組件互不影響

1、引入swiper

npm i swiper@5.2.0

2、創(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.name}}</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, {
        loop: true, // 循環(huán)模式選項(xiàng)
        autoHeight:'true',//開啟自適應(yīng)高度,容器高度由slide高度決定
        //分頁(yè)器
        pagination: {
          el: '.swiper-pagination',
          clickable:true,//分頁(yè)器按鈕可點(diǎn)擊
        },
        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: 3000,
          stopOnLastSlide: false,
          disableOnInteraction: false
        },
        slidesPerView: 1, //視口展示slide數(shù)1
        slidesPerGroup: 1, //slide數(shù)1頁(yè)一組
      })

    },
    //銷毀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;
  }
  /*自定義分頁(yè)器按鈕被點(diǎn)擊選中時(shí)的樣式*/
  /deep/.swiper-pagination-bullet-active{
    background-color: #d5a72f !important;
    width: 20px;
  }
  /*自定義分頁(yè)器按鈕常規(guī)樣式*/
  /deep/.swiper-pagination-bullet{
    background-color: #9624bf;
    opacity: 1;
    width: 20px;
  }
</style>

3、創(chuàng)建父組件App.vue渲染多個(gè)swiper組件、模擬異步數(shù)據(jù)變化

<template>
  <div id="app">
    <!--傳遞不同的currentIndex 作為區(qū)分不同swiper組件的動(dòng)態(tài)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
    //延時(shí)模擬兩次數(shù)據(jù)變化
    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>

只需要這兩個(gè)文件就可以vue項(xiàng)目中運(yùn)行demo查看效果了

到此這篇關(guān)于Vue使用Swiper看這一篇就夠了的文章就介紹到這了,更多相關(guān)Vue使用Swiper內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue 3.x 中mixin封裝公用方法應(yīng)用方式

    vue 3.x 中mixin封裝公用方法應(yīng)用方式

    這篇文章主要介紹了vue 3.x 中mixin封裝公用方法應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue-cli腳手架-bulid下的配置文件

    vue-cli腳手架-bulid下的配置文件

    這篇文章主要介紹了vue-cli腳手架-bulid下的配置文件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • vue3+elementui-plus實(shí)現(xiàn)一個(gè)接口上傳多個(gè)文件功能

    vue3+elementui-plus實(shí)現(xiàn)一個(gè)接口上傳多個(gè)文件功能

    這篇文章主要介紹了vue3+elementui-plus實(shí)現(xiàn)一個(gè)接口上傳多個(gè)文件,先使用element-plus寫好上傳組件,然后假設(shè)有個(gè)提交按鈕,點(diǎn)擊上傳文件請(qǐng)求接口,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • Vue+Element UI實(shí)現(xiàn)概要小彈窗的全過(guò)程

    Vue+Element UI實(shí)現(xiàn)概要小彈窗的全過(guò)程

    彈窗效果是我們?nèi)粘i_發(fā)中經(jīng)常遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于Vue+Element UI實(shí)現(xiàn)概要小彈窗的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • element用腳本自動(dòng)化構(gòu)建新組件的實(shí)現(xiàn)示例

    element用腳本自動(dòng)化構(gòu)建新組件的實(shí)現(xiàn)示例

    本文主要介紹了element-ui的用腳本自動(dòng)化構(gòu)建新組件的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 使用vue封裝一個(gè)自定義樣式的滾動(dòng)條

    使用vue封裝一個(gè)自定義樣式的滾動(dòng)條

    眾所周知,當(dāng)容器高度固定而內(nèi)容部分高度超出容器高度時(shí),瀏覽器會(huì)渲染出一個(gè)可以滾動(dòng)并用于顯示剩余界面的條 -- 滾動(dòng)條,它可以簡(jiǎn)單的樣式修改,但是位置是固定的,無(wú)法移動(dòng),而我們需要改變位置的時(shí)候,它就不能滿足我們的需求了,這時(shí)我們可以自己手寫一個(gè)
    2023-10-10
  • 如何使用vue過(guò)濾器filter

    如何使用vue過(guò)濾器filter

    這篇文章主要介紹了如何使用vue過(guò)濾器filter,對(duì)vue感興趣的同學(xué),可以參考下
    2021-05-05
  • vue props 一次傳多個(gè)值實(shí)例

    vue props 一次傳多個(gè)值實(shí)例

    這篇文章主要介紹了vue props 一次傳多個(gè)值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • 在vue-cli3中使用axios獲取本地json操作

    在vue-cli3中使用axios獲取本地json操作

    這篇文章主要介紹了在vue-cli3中使用axios獲取本地json操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Vue使得大屏自適應(yīng)的多種方法

    Vue使得大屏自適應(yīng)的多種方法

    這篇文章主要介紹了Vue使得大屏自適應(yīng)的多種方法,自適屏幕,始終保持16:9的比例,還一種是使用CSS scale屬性對(duì)大屏幕做自適應(yīng)處理,需要的朋友可以參考下
    2023-10-10

最新評(píng)論