vue3封裝輪播圖組件的方法
目的
封裝輪播圖組件,直接使用,具體內容如下
大致步驟
- 準備my-carousel組件基礎布局,全局注冊
- 準備home-banner組件,使用my-carousel組件,再首頁注冊使用。
- 深度作用選擇器覆蓋my-carousel組件的默認樣式
- 在home-banner組件獲取輪播圖數(shù)據(jù),傳遞給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)
// 定時器方法,實現(xiàn)自動輪播效果
const autoplayFn = () => {
// 防抖,防止多次觸發(fā)定時器
clearInterval(timer.value)
timer.value = setInterval(() => {
index.value += 1
if (index.value >= props.findBannerList.length) {
index.value = 0
}
}, props.duration * 1000)
}
// 偵聽器,根據(jù)接口返回的數(shù)據(jù)與傳遞的相關屬性參數(shù) autoplay 開啟輪播
// 監(jiān)聽返回的數(shù)據(jù)的長度,當長度大于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()
}
}
// 點擊輪播圖上的左右按鈕,切換輪播圖,通過傳遞進來的參數(shù),決定輪播圖往左往右
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 組件, 下面的參數(shù)可以在 home-banner 組件中設置
findBannerList 參數(shù)作為,后臺請求數(shù)據(jù)給到組件內部
autoplay 參數(shù)是否開啟輪播,默認 true 開啟輪播
duration 參數(shù)輪播停留時間間隔以 秒 為單位
<template>
<div class="home-banner">
<MyCarousel :findBannerList="findBannerList" :autoplay="true" :duration="3" />
</div>
</template>
總結
按照思路步驟,一步步實現(xiàn)即可。
1.基本組件拆分和布局
2.自動輪播
3.懸停控制啟動和停止
4.手動控制切換
5.銷毀定時器
6.抽取相關的參數(shù)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue3+elementui-plus實現(xiàn)一個接口上傳多個文件功能
這篇文章主要介紹了vue3+elementui-plus實現(xiàn)一個接口上傳多個文件,先使用element-plus寫好上傳組件,然后假設有個提交按鈕,點擊上傳文件請求接口,本文結合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07
區(qū)分vue-router的hash和history模式
這篇文章主要介紹了區(qū)分vue-router的hash和history模式,幫助大家更好的理解和學習vue路由,感興趣的朋友可以了解下2020-10-10
Vue聲明式導航與編程式導航及導航守衛(wèi)和axios攔截器全面詳細講解
這篇文章主要介紹了Vue聲明式導航與編程式導航及導航守衛(wèi)和axios攔截器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01
vue2中,根據(jù)list的id進入對應的詳情頁并修改title方法
今天小編就為大家分享一篇vue2中,根據(jù)list的id進入對應的詳情頁并修改title方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue圓環(huán)百分比進度條組件功能的實現(xiàn)
在一些頁面設置進度條效果給人一種很好的體驗效果,今天小編教大家vue圓環(huán)百分比進度條組件功能的實現(xiàn)代碼,代碼超級簡單啊,感興趣的朋友快來看下吧2021-05-05

