vue3封装轮播图组件的方法
作者:新时代农民工Top 发布时间:2024-06-07 16:03:16
标签:vue3,轮播图
目的
封装轮播图组件,直接使用,具体内容如下
大致步骤
准备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
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 如果你在 ForeignKey或 ManyToManyField字段上使用 related_name属性,你必须总是为该字段指定一个唯一的反
- 使用xlrd模块和xlwt模块解题思想:xlwt模块是非追加写.xls的模块,所以要借助for循环和列表,来一次性写入,这样就没有追加与非追
- 1. 概念显著性检测,就是使用图像处理技术和计算机视觉算法来定位图片中最“显著”的区域。显著区域就是
- 这篇文章主要介绍了微信小程序 textarea 层级过高问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 分布式锁一般有三种实现方式:1. 数据库乐观锁;2. 基于Redis的分布式锁;3. 基于ZooKeeper的分布式锁。本篇博客将介绍第二种
- 1.GO中包的定义与介绍go中包分为三种:1.系统内置包 2. 自定义包 3.第三方包2. 包管理工具 go mod2.1 自定义包 (可以
- 1.准备工作下载mysql的最新免安装版本mysql-noinstall-5.1.53-win32.zip,解压缩到相关目录,如:d:\\
- 简介在SQL SERVER中,数据库在硬盘上的存储方式和普通文件在Windows中的存储方式没有什么不同,仅仅是几个文件而已.SQL SER
- 在Python语言中最常见的括号有三种,分别是:小括号()、中括号[]、花括号{};其作用也不相同,分别用来代表不同的Python基本内置数
- 继续分享pygame有趣的技术知识,欢迎往下看。一、先搭个架子(一)黏贴背景图:实现代码如下:import pygamepygame.ini
- 每年意甲德甲英超西甲各大联赛的赛程表都是球迷们的必看之物,想起之前写过的一段生成赛程表的代码,用Python来写这类东西太舒服了。这个算法叫
- 本文实例讲述了Python实现自动登录百度空间的方法。分享给大家供大家参考,具体如下:开发环境:Fedora12 + Python2.6.2
- FFmpeg简介FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录
- 阅读上一章:打印样式Chapter 12 CSS布局本书到此为止,讨论的主要是页面内部元素,也就是内容,但是大结构怎么办?长久以来,设计者都
- MySQL 系统会在内存(MEMORY)和磁盘(MyISAM)中建立临时表,如何能知道在磁盘中建立了多少临时表以及在内存中建立多
- 1.最大值max(3,4) ##运行结果为42.最小值min(3,4) ##运行结果为33.求和sum(range
- Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串、数字、元组等其他容器模型。字典由键和对应值成对组成。字典也被称作关联数
- 本文实例讲述了Python实现根据IP地址和子网掩码算出网段的方法。分享给大家供大家参考。具体如下:该代码在Linux环境2.6.6pyth
- 引言之前松哥写过一个 MySQL 系列,但是当时是基于 MySQL5.7 的,最近有空在看 MySQL8 的文档,发现和 MySQL5.7
- 这篇文章主要介绍了python操作openpyxl导出Excel 设置单元格格式及合并处理代码实例,文中通过示例代码介绍的非常详细,对大家的