广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue使用Swiper的案例详解
  • 576
分享到

Vue使用Swiper的案例详解

2024-04-02 19:04:59 576人浏览 独家记忆
摘要

Vue使用Swiper看这一篇就够了 此案例实现需求 完成swiper动态异步数据下的slide渲染自定义分页器样式解决loop:true设置时的事件丢失问题swiper鼠标移入/移

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;
  }
  
  .my-swiper-slide{
    height: 300px;
    background-color: pink;
  }
  
  .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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Vue使用Swiper的案例详解

本文链接: https://www.lsjlt.com/news/152286.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vue使用Swiper的案例详解
    Vue使用Swiper看这一篇就够了 此案例实现需求 完成swiper动态异步数据下的slide渲染自定义分页器样式解决loop:true设置时的事件丢失问题swiper鼠标移入/移...
    99+
    2022-11-13
  • vue之keepAlive使用案例详解
    在开发中经常有从列表跳到详情页,然后返回详情页的时候需要缓存列表页的状态(比如滚动位置信息),这个时候就需要保存状态,要缓存状态;vue里提供了keep-alive组件用来缓存状态。...
    99+
    2022-11-12
  • 在vue项目中使用Swiper插件详解
    目录vue项目使用Swiper插件第一步:下载Swiper插件 第二步:在对应的组件页面中导入该插件第三步: 编写对应的html部分第四步:在data中配置对应的参数vue...
    99+
    2023-01-14
    vue Swiper vue使用Swiper插件 vue Swiper插件
  • vue之监听器的使用案例详解
    第一种,用jquery的ajax发请求  用户注册时,判断用户名不能重复,可以用到监听器。监听器,用watch,需要监听哪个值的变化,就把这个值,放在watch里面。&#...
    99+
    2022-11-12
  • Vue3使用Swiper实现轮播图示例详解
    目录正文Swiper 的参数代码实现Error: Can‘t resolve ‘swiper/css/swiper.css‘ 如何解决其它方式正文...
    99+
    2023-02-12
    Vue3 Swiper轮播图 Vue3 Swiper
  • Vue之TodoList案例详解
    <template> <div id="root"> <div class="todo-container"> ...
    99+
    2022-11-12
  • SpringCloudFeign的使用案例详解
    目录Feign简介使用传参拓展配置超时、连接时间日志打印Feign简介 Feign是Netflix开发的⼀个轻量级RESTful的HTTP服务客户端(⽤它来发起请求,远程调⽤的),是...
    99+
    2023-02-09
    Spring Cloud Feign使用 Spring Cloud Feign
  • Vue使用Swiper封装轮播图组件的方法详解
    目录Swiper为什么要封装组件开始封装1.下载安装Swiper2.引入css样式文件3.引入js文件4.把官网使用方法中的HTML结构复制粘贴过来5.初始化Swiper自定义效果完...
    99+
    2022-11-13
  • CrashRpt使用案例详解
    CrashRpt介绍及简单应用 1、简介 CrashRpt是一个开源的第三方包,在程序出现未处理异常时,能够收集错误信息,并生成程序错误报告。CrashRpt可以将报告按照指定的方式...
    99+
    2022-11-12
  • MySql escape的使用案例详解
    MySQL转义 转义即表示转义字符原来的语义,一个转义字符的目的是开始一个字符序列,使得转义字符开头的该字符序列具有不同于该字符序列单独出现时的语义。 在sql like语句中,比如...
    99+
    2022-11-12
  • vue3 teleport的使用案例详解
    官网 https://cli.vuejs.org/zh/guide/ 有时组件模板的一部分逻辑上属于该组件,而从技术角度来看,最好将模板的这一部分移动到 DOM 中 Vue app ...
    99+
    2022-11-12
  • vue使用Swiper踩坑解决避坑
    目录我的Swiper定义:报错信息:保留默认名class:swiper-container查看GitHub我的Swiper定义: Failed to execute 'get...
    99+
    2023-05-20
    vue Swiper踩坑解决 vue Swiper避坑
  • Vue使用swiper问题怎么解决
    本文小编为大家详细介绍“Vue使用swiper问题怎么解决”,内容详细,步骤清晰,细节处理妥当,希望这篇“Vue使用swiper问题怎么解决”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。一、下载指定版本swipe...
    99+
    2023-07-06
  • vue使用swiper插件实现轮播图的示例
    目录vue - 使用swiper插件实现轮播图 使用watch与$nextTick解决轮播的Bug hello大家好,最近我在做一个仿饿了么的项目,我会将我的项目经验同步到这里,与大...
    99+
    2022-11-12
  • Android ActivityManager使用案例详解
    前言 Activity可以获取运行中的应用信息,可以获取到servcie,process,app,memory,Task信息等。 获取信息 ActivityManager....
    99+
    2022-11-12
  • Android Handler使用案例详解
    什么是Handler? Handler可以发送和处理消息对象或Runnable对象,这些消息对象和Runnable对象与一个线程相关联。每个Handler的实例都关联了一个线程和线程...
    99+
    2022-11-12
  • C# PropertyGrid使用案例详解
    1. 只有public的property能显示出来,可以通过BrowsableAttribute来控制是否显示,通过CategoryAttribute设置分类,通过Descripti...
    99+
    2022-11-12
  • C# AttributeUsage使用案例详解
    C# AttributeUsage的使用是如何的呢?首先让我们来了解一下什么是AttributeUsage类它是另外一个预定义特性类,AttributeUsage类的作用就是帮助我们...
    99+
    2022-11-12
  • Android VelocityTracker使用案例详解
       VelocityTracker顾名思义即速度跟踪,在android中主要应用于touch even。VelocityTracker通过跟踪一连串事件实时计算...
    99+
    2022-11-12
  • IOS NSTimeInterval使用案例详解
    一 ios 获取时间间隔 想在程序开始或者进入某个界面 ,到结束程序或退出某个界面,获取到这个持续时间. 获取到这个时间还需要转化一个取得时分秒. -(NSString *)ge...
    99+
    2022-06-01
    IOS NSTimeInterval
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作