Vue使用Swiper的案例详解

 更新时间:2022年06月22日 12:01:24   作者:Tyler Yue  
这篇文章主要介绍了Vue使用Swiper的案例详解,主要包括引入swiper,创建轮播图组件CarouselContainer.vue的详细代码,本文给大家介绍的非常详细,需要的朋友可以参考下

Vue使用Swiper看这一篇就够了

此案例实现需求

  • 完成swiper动态异步数据下的slide渲染
  • 自定义分页器样式
  • 解决loop:true设置时的事件丢失问题
  • swiper鼠标移入/移出 暂停/开始轮播
  • 单页面渲染多个swiper组件互不影响

1、引入swiper

npm i swiper@5.2.0

2、创建轮播图组件CarouselContainer.vue,详细解析在代码注释中

<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>

3、创建父组件App.vue渲染多个swiper组件、模拟异步数据变化

<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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue框架之goods组件开发详解

    Vue框架之goods组件开发详解

    这篇文章主要介绍了Vue框架之goodvs组件开发详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Element InputNumber 计数器的实现示例

    Element InputNumber 计数器的实现示例

    这篇文章主要介绍了Element InputNumber 计数器的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • vue实现气泡运动撞击效果

    vue实现气泡运动撞击效果

    这篇文章主要为大家详细介绍了vue实现气泡运动撞击效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • element table多层嵌套显示的实践

    element table多层嵌套显示的实践

    本文主要介绍了element table多层嵌套显示的实践,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Vue 3 表单与后端数据交互之查询并回显数据步骤流程

    Vue 3 表单与后端数据交互之查询并回显数据步骤流程

    本文给大家介绍Vue3表单与后端数据交互之查询并回显数据步骤流程,结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-12-12
  • vue在使用ECharts时的异步更新和数据加载详解

    vue在使用ECharts时的异步更新和数据加载详解

    这篇文章主要给大家介绍了关于vue在使用ECharts时的异步更新和数据加载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • Vue自定义指令学习及应用详解

    Vue自定义指令学习及应用详解

    这篇文章主要为大家详细介绍了Vue中自定义指令的学习以及如何利用Vue制作一个简单的学生信息管理系统,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-05-05
  • vuex中store存储store.commit和store.dispatch的用法

    vuex中store存储store.commit和store.dispatch的用法

    这篇文章主要介绍了vuex中store存储store.commit和store.dispatch的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • 关于vue-router路径计算问题

    关于vue-router路径计算问题

    这篇文章主要介绍了关于vue-router路径计算问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • 使用Vue3实现羊了个羊的算法

    使用Vue3实现羊了个羊的算法

    这篇文章主要介绍了使用Vue3实现羊了个羊的算法,初始化的随机位置算法,通过实例代码介绍了计算偏移量的方法,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09

最新评论