Vue使用Swiper的案例详解
作者:Tyler 发布时间:2024-04-26 17:39:01
标签:Vue,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;
}
/*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查看效果了
来源:https://blog.csdn.net/qq_43137699/article/details/125375095


猜你喜欢
- 上个周末去书店时碰巧看到了AS3 CookeBook,我记得在apollo的alpha版快出来的时候,7yue就推荐过这个小册子,只不过我已
- 1.简介(torch.nn下的)卷积层主要使用的有3类,用于处理不同维度的数据参数 Parameters:in_channels(int)
- 一、什么是Python类?python中的类是创建特定对象的蓝图。它使您可以以特定方式构建软件。问题来了,怎么办?类允许我们以一种易于重用的
- 注意事项Soft-NMS对于大多数数据集而言,作用比较小,提升效果非常不明显,它起作用的地方是大量密集的同类重叠场景,大量密集的不同类重叠场
- SWFUpload上传组件,最初由Vinterwebb.se开发,组件主体由Flash与JavaScript整合而成,主要致力解决多文件、大
- asp防止用户同时登陆的方法,实现这个功能可有两种方式:1.使用application用application对象:如果做的是大型社区,可能
- 一段非常简单代码普通调用方式def console1(a, b): print("进入函数")
- 介绍使用subprocess模块的目的是用于替换os.system等一些旧的模块和方法。运行python的时候,我们都是在创建并运行一个进程
- 字典排序在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的。因此,为了使统计得到的结果更方便查看需要进行排序。
- 前言没想到python是如此强大,令人着迷,以前看见图片总是一张一张复制粘贴,现在好了,学会python就可以用程序将一张张图片,保存下来。
- 本文实例讲述了PHP类的特性。分享给大家供大家参考,具体如下:对象向下传递特性当一个对象调用一个实例方法,然后在该方法中又去静态调用另一个类
- 前一阵,我在为朋友编写一个源代码监控程序的时候,发现了一个 Python 领域非常简单好用的图形界面库。说起图形界面库,你可能会想到 TkI
- 对shuffle=True的理解:之前不了解shuffle的实际效果,假设有数据a,b,c,d,不知道batch_size=2后打乱,具体是
- 附加数据库出错:无法打开物理文件 "XXXXXXXXXXXXX"。操作系统错误 5:"5(拒绝访问。)&quo
- 无论是公司的同事还是外界的程序员朋友们,大部分人对JavaScript的高级应用不甚了解,已有的知识架构里会认为JavaScript仅仅是一
- 性能首先,FCKEDITOR的性能是非常好的,用户只需很少的时间就可以载入FCKEDITOR所需文件.对于其他在线编辑器来说,这几乎是个很难
- 从publish 表中取出第 n 条到第 m 条的记录的sql语句写法:SELECT TOP m-n+1 *&
- 1.交换变量x = 6y = 5x, y = y, xprint x>>> 5print y>>> 62
- isnull()Null 值指出变量不包含有效数据。Null 与 Empty 不同,后者指出变量未经初始化。Null 与零长度字符串 (&q
- 如题:在python的函数调用中需要记录时间,下面是记录毫秒时间的方法。import datetimeimport timet1 = dat