vue3封装轮播图组件的方法
作者:新时代农民工Top 时间:2024-06-07 16:03:16
目的
封装轮播图组件,直接使用,具体内容如下
大致步骤
准备my-carousel组件基础布局,全局注册
准备home-banner组件,使用my-carousel组件,再首页注册使用。
深度作用选择器覆盖my-carousel组件的默认样式
在home-banner组件获取轮播图数据,传递给my-carousel组件
在my-carousel组件完成渲染
自动播放,暴露自动轮播属性,设置了就自动轮播
如果有自动播放,鼠标进入离开,暂停,开启
指示器切换,上一张,下一张
销毁组件,清理定时器
落地代码
一、封装组件
<template>
<div class="my-carousel" @mouseenter="stop" @mouseleave="start">
<ul class="carousel-body">
<li v-for="(item, i) in findBannerList" :key="item.id" class="carousel-item" :class="{ fade: index === i }">
<RouterLink to="/">
<img :src="item.imgUrl" alt="图片" />
</RouterLink>
</li>
</ul>
<a @click="clickFn(-1)" href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
<a @click="clickFn(1)" href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
<div class="carousel-indicator">
<span @click="active(i)" v-for="(item, i) in findBannerList" :key="i" :class="{ active: index === i }"></span>
</div>
</div>
</template>
<script>
import { onUnmounted, ref, watch } from 'vue'
export default {
name: 'Carousel',
props: {
findBannerList: {
type: Array,
default: () => []
},
autoplay: {
type: Boolean,
default: true
},
duration: {
type: Number,
default: 3
}
},
setup(props) {
const index = ref(0)
// 定义一个常量存储定时器
const timer = ref(null)
// 定时器方法,实现自动轮播效果
const autoplayFn = () => {
// 防抖,防止多次触发定时器
clearInterval(timer.value)
timer.value = setInterval(() => {
index.value += 1
if (index.value >= props.findBannerList.length) {
index.value = 0
}
}, props.duration * 1000)
}
// * ,根据接口返回的数据与传递的相关属性参数 autoplay 开启轮播
// 监听返回的数据的长度,当长度大于1的时候并且 autoplay 的为 true 的时候开启轮播
watch(
() => props.findBannerList,
() => {
if (props.findBannerList.length > 1 && props.autoplay) {
autoplayFn()
}
}
)
// 鼠标移入轮播图,停止自动播放
const stop = () => {
if (timer.value) clearInterval(timer.value)
}
// 鼠标移出轮播图,开启定时器
const start = () => {
if (props.findBannerList.length > 1 && props.autoplay) {
autoplayFn()
}
}
// 点击轮播图上的左右按钮,切换轮播图,通过传递进来的参数,决定轮播图往左往右
const clickFn = e => {
index.value += e
if (index.value >= props.findBannerList.length) {
index.value = 0
}
if (index.value < 0) {
index.value = props.findBannerList.length - 1
}
}
// 点击指示器(轮播图底下的小点)切换轮播图
const active = e => {
index.value = e
}
// 组件销毁的时候情书定时器,避免性能损耗
onUnmounted(() => {
if (timer.value) clearInterval(timer.value)
})
return { index, stop, start, clickFn, active }
}
}
</script>
<style scoped lang="less">
.my-carousel {
width: 100%;
height: 100%;
min-width: 300px;
min-height: 150px;
position: relative;
.carousel {
&-body {
width: 100%;
height: 100%;
}
&-item {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
transition: opacity 0.5s linear;
&.fade {
opacity: 1;
z-index: 1;
}
img {
width: 100%;
height: 100%;
}
}
&-indicator {
position: absolute;
left: 0;
bottom: 20px;
z-index: 2;
width: 100%;
text-align: center;
span {
display: inline-block;
width: 12px;
height: 12px;
background: rgba(0, 0, 0, 0.2);
border-radius: 50%;
cursor: pointer;
~ span {
margin-left: 12px;
}
&.active {
background: #fff;
}
}
}
&-btn {
width: 44px;
height: 44px;
background: rgba(0, 0, 0, 0.2);
color: #fff;
border-radius: 50%;
position: absolute;
top: 228px;
z-index: 2;
text-align: center;
line-height: 44px;
opacity: 0;
transition: all 0.5s;
&.prev {
left: 20px;
}
&.next {
right: 20px;
}
}
}
&:hover {
.carousel-btn {
opacity: 1;
}
}
}
</style>
二、封装成插件
import MyCarousel from './my-carousel.vue'
export default {
install(app) {
app.component(MyCarousel.name, MyCarousel)
}
}
三、在入口文件 main.js 中全局注册
import { createApp } from 'vue'
import App from './App.vue'
import MyUI from './components/library'
// 插件的使用,在main.js使用app.use(插件)
createApp(App).use(MyUI).mount('#app')
四、在项目中使用组件
准备home-banner组件,使用my-carousel组件,然后在项目中使用轮播的地方引入 home-banner 组件, 下面的参数可以在 home-banner 组件中设置
findBannerList 参数作为,后台请求数据给到组件内部
autoplay 参数是否开启轮播,默认 true 开启轮播
duration 参数轮播停留时间间隔以 秒 为单位
<template>
<div class="home-banner">
<MyCarousel :findBannerList="findBannerList" :autoplay="true" :duration="3" />
</div>
</template>
来源:https://blog.csdn.net/web00_11/article/details/120290805
标签:vue3,轮播图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何在向量化NumPy数组上进行移动窗口
2023-04-23 08:26:55
![](https://img.aspxhome.com/file/2023/3/68893_0s.png)
Python3爬虫中识别图形验证码的实例讲解
2022-10-17 17:11:20
![](https://img.aspxhome.com/file/2023/3/90383_0s.png)
DW实现滚动新闻
2007-12-03 11:35:00
Python函数基础(定义函数、函数参数、匿名函数)
2022-04-24 05:21:41
SpringBoot Logback日志记录到数据库的实现方法
2024-01-16 11:58:11
![](https://img.aspxhome.com/file/2023/4/81964_0s.jpg)
用Dreamweaver设计自动关闭的网页
2010-09-02 12:29:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113111210s.jpg)
Mootools常用方法扩展(五)
2009-03-03 12:12:00
PS笔刷,样式,形状、渐变、滤镜载入方式及使用
2007-10-17 11:47:00
![](https://img.aspxhome.com/file/UploadPic/200710/17/2007101711564849s.gif)
使用VSCODE配置GO语言开发环境的完整步骤
2024-04-27 15:27:51
Go实现用户每日限额的方法(例一天只能领三次福利)
2024-05-22 10:20:09
![](https://img.aspxhome.com/file/2023/6/123866_0s.png)
小谈MySQL字符集
2009-02-13 13:30:00
Golang中的错误处理深入分析
2024-02-18 20:59:46
Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法
2023-12-19 22:42:41
JavaScript 轮播图和自定义滚动条配合鼠标滚轮分享代码贴
2024-04-29 13:40:59
Django框架使用内置方法实现登录功能详解
2021-11-01 00:54:50
![](https://img.aspxhome.com/file/2023/6/133526_0s.png)
Vue CLI2升级至Vue CLI3的方法步骤
2024-06-05 10:03:25
![](https://img.aspxhome.com/file/2023/4/122004_0s.jpg)
Web标准的web UI
2008-01-02 12:34:00
浅析python 中大括号中括号小括号的区分
2021-06-15 15:14:16
用CSS定义 li 样式
2007-09-28 20:56:00
一个ASP站内搜索的实例源代码
2007-09-21 12:13:00