vue3封裝輪播圖組件的方法
目的
封裝輪播圖組件,直接使用,具體內(nèi)容如下
大致步驟
- 準(zhǔn)備my-carousel組件基礎(chǔ)布局,全局注冊(cè)
- 準(zhǔn)備home-banner組件,使用my-carousel組件,再首頁(yè)注冊(cè)使用。
- 深度作用選擇器覆蓋my-carousel組件的默認(rèn)樣式
- 在home-banner組件獲取輪播圖數(shù)據(jù),傳遞給my-carousel組件
- 在my-carousel組件完成渲染
- 自動(dòng)播放,暴露自動(dòng)輪播屬性,設(shè)置了就自動(dòng)輪播
- 如果有自動(dòng)播放,鼠標(biāo)進(jìn)入離開(kāi),暫停,開(kāi)啟
- 指示器切換,上一張,下一張
- 銷(xiāo)毀組件,清理定時(shí)器
落地代碼
一、封裝組件
<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) // 定義一個(gè)常量存儲(chǔ)定時(shí)器 const timer = ref(null) // 定時(shí)器方法,實(shí)現(xiàn)自動(dòng)輪播效果 const autoplayFn = () => { // 防抖,防止多次觸發(fā)定時(shí)器 clearInterval(timer.value) timer.value = setInterval(() => { index.value += 1 if (index.value >= props.findBannerList.length) { index.value = 0 } }, props.duration * 1000) } // 偵聽(tīng)器,根據(jù)接口返回的數(shù)據(jù)與傳遞的相關(guān)屬性參數(shù) autoplay 開(kāi)啟輪播 // 監(jiān)聽(tīng)返回的數(shù)據(jù)的長(zhǎng)度,當(dāng)長(zhǎng)度大于1的時(shí)候并且 autoplay 的為 true 的時(shí)候開(kāi)啟輪播 watch( () => props.findBannerList, () => { if (props.findBannerList.length > 1 && props.autoplay) { autoplayFn() } } ) // 鼠標(biāo)移入輪播圖,停止自動(dòng)播放 const stop = () => { if (timer.value) clearInterval(timer.value) } // 鼠標(biāo)移出輪播圖,開(kāi)啟定時(shí)器 const start = () => { if (props.findBannerList.length > 1 && props.autoplay) { autoplayFn() } } // 點(diǎn)擊輪播圖上的左右按鈕,切換輪播圖,通過(guò)傳遞進(jìn)來(lái)的參數(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 } } // 點(diǎn)擊指示器(輪播圖底下的小點(diǎn))切換輪播圖 const active = e => { index.value = e } // 組件銷(xiāo)毀的時(shí)候情書(shū)定時(shí)器,避免性能損耗 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 中全局注冊(cè)
import { createApp } from 'vue' import App from './App.vue' import MyUI from './components/library' // 插件的使用,在main.js使用app.use(插件) createApp(App).use(MyUI).mount('#app')
四、在項(xiàng)目中使用組件
準(zhǔn)備home-banner組件,使用my-carousel組件,然后在項(xiàng)目中使用輪播的地方引入 home-banner 組件, 下面的參數(shù)可以在 home-banner 組件中設(shè)置
findBannerList 參數(shù)作為,后臺(tái)請(qǐng)求數(shù)據(jù)給到組件內(nèi)部
autoplay 參數(shù)是否開(kāi)啟輪播,默認(rèn) true 開(kāi)啟輪播
duration 參數(shù)輪播停留時(shí)間間隔以 秒 為單位
<template> <div class="home-banner"> <MyCarousel :findBannerList="findBannerList" :autoplay="true" :duration="3" /> </div> </template>
總結(jié)
按照思路步驟,一步步實(shí)現(xiàn)即可。
1.基本組件拆分和布局
2.自動(dòng)輪播
3.懸停控制啟動(dòng)和停止
4.手動(dòng)控制切換
5.銷(xiāo)毀定時(shí)器
6.抽取相關(guān)的參數(shù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+elementui-plus實(shí)現(xiàn)一個(gè)接口上傳多個(gè)文件功能
這篇文章主要介紹了vue3+elementui-plus實(shí)現(xiàn)一個(gè)接口上傳多個(gè)文件,先使用element-plus寫(xiě)好上傳組件,然后假設(shè)有個(gè)提交按鈕,點(diǎn)擊上傳文件請(qǐng)求接口,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07區(qū)分vue-router的hash和history模式
這篇文章主要介紹了區(qū)分vue-router的hash和history模式,幫助大家更好的理解和學(xué)習(xí)vue路由,感興趣的朋友可以了解下2020-10-10Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器全面詳細(xì)講解
這篇文章主要介紹了Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Vue實(shí)現(xiàn)一個(gè)返回頂部backToTop組件
本篇文章主要介紹了Vue實(shí)現(xiàn)一個(gè)返回頂部backToTop組件,可以實(shí)現(xiàn)回到頂部效果,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07vue2中,根據(jù)list的id進(jìn)入對(duì)應(yīng)的詳情頁(yè)并修改title方法
今天小編就為大家分享一篇vue2中,根據(jù)list的id進(jìn)入對(duì)應(yīng)的詳情頁(yè)并修改title方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08vue如何實(shí)現(xiàn)對(duì)請(qǐng)求參數(shù)進(jìn)行簽名
這篇文章主要介紹了vue如何實(shí)現(xiàn)對(duì)請(qǐng)求參數(shù)進(jìn)行簽名問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01vue圓環(huán)百分比進(jìn)度條組件功能的實(shí)現(xiàn)
在一些頁(yè)面設(shè)置進(jìn)度條效果給人一種很好的體驗(yàn)效果,今天小編教大家vue圓環(huán)百分比進(jìn)度條組件功能的實(shí)現(xiàn)代碼,代碼超級(jí)簡(jiǎn)單啊,感興趣的朋友快來(lái)看下吧2021-05-05