目錄
- 前言
- 一、為什么封裝?
- 二、如何封裝?
- 1. 準備
- 2. 注冊為全域組件
- 3. 使用演示
- 總結
前言
vue3專案中如何封裝自己的輪播圖效果組件呢,希望輪播圖可以控制是否自動輪播,當滑鼠懸停的時候停止自動輪播,點擊左右切換或者分頁指示器可以切換圖片,這些功能是如何實作的呢?又是如何通過插件方式注冊為全域組件的呢?一起來看看如何實作的吧~
一、為什么封裝?
- 為了迎合es6模塊化開發思想
- 注冊為全域組件,可以更好地復用,需要用到的地方,直接使用標簽即可
二、如何封裝?
1. 準備
像之前vue3——自己封裝骨架屏效果這篇博文一樣的操作,通過vue插件的方式注冊為全域組件,
專案中需要用到的全域組件可以統一都放到src/components目錄下,新建xx.vue檔案,名稱自定義,
代碼如下(示例):
<template>
<div class='carousel' @mouseenter="stop()" @mouseleave="start()">
<ul class="carousel-body">
<li class="carousel-item" v-for="(item, i) in list" :key="item.id" :class="{fade: index === i}">
<RouterLink to="/">
<img :src="item.imgUrl" alt="">
</RouterLink>
</li>
</ul>
<!-- 左右控制按鈕 -->
<a href="javascript:;" class="carousel-btn prev" @click="toggle(-1)"><</a>
<a href="javascript:;" class="carousel-btn next" @click="toggle(1)">></a>
<!-- 分頁器 -->
<div class="carousel-indicator">
<span v-for="(item,i) in list" :key="i" :class="{active: index === i}" @click="index = i"></span>
</div>
</div>
</template>
<script>
import { ref, watch, onUnmounted } from 'vue'
export default {
name: 'Carousel',
props: {
list: {
type: Array,
default: () => []
},
duration: {
type: Number,
default: 3000
},
autoPlay: {
type: Boolean,
default: false
}
},
setup (props) {
const index = ref(0)
let timer = null
// 自動播放
const autoPlayFn = () => {
clearInterval(timer)
timer = setInterval(() => {
index.value++
if (index.value >= props.list.length) {
index.value = 0
}
}, props.duration)
}
// 滑鼠進入停止,移出開啟自動,前提條件:autoPlay為true
const stop = () => {
if (timer) clearInterval(timer)
}
const start = () => {
if (props.list.length && props.autoPlay) {
autoPlayFn()
}
}
// 輪播圖左右按鈕切換效果
const toggle = (step) => {
index.value += step
// 確定右側臨界值
if (index.value >= props.list.length) {
index.value = 0
return
}
// 確定左側臨界值
if (index.value < 0) {
index.value = props.list.length - 1
}
}
watch(() => props.list, (newVal) => {
// 有資料&開啟自動播放,才呼叫自動播放函式
if (newVal.length > 1 && props.autoPlay) {
index.value = 0
autoPlayFn()
}
}, { immediate: true })
// 組件消耗,清理定時器
onUnmounted(() => {
clearInterval(timer)
})
return { index, stop, start, toggle }
}
}
</script>
<style scoped lang="less">
.carousel{
width: 100%;
height: 100%;
min-width: 300px;
min-height: 150px;
position: relative;
border: 1px solid #ccc;
text-decoration: none;
.carousel{
&-body {
width: 100%;
height: 100%;
}
&-item {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
list-style: none;
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,.2);
color: #fff;
border-radius: 50%;
position: absolute;
top: 228px;
z-index: 2;
text-align: center;
line-height: 44px;
opacity: 0;
font-family: serif;
text-decoration: none;
transition: all 0.5s;
&.prev{
left: 20px;
}
&.next{
right: 20px;
}
}
}
&:hover {
.carousel-btn {
opacity: 1;
}
}
}
</style>
2. 注冊為全域組件
在src/components目錄下新建index.js檔案
代碼如下(示例):
import xx from './library/xx.vue'
// vue2.0插件寫法要素:匯出一個物件,有install函式,默認傳入了Vue建構式,Vue基礎之上擴展
// vue3.0插件寫法要素:匯出一個物件,有install函式,默認傳入了app應用實體,app基礎之上擴展
export default {
install (app) {
app.component(xx.name, xx)
}
}
在main.js中注冊為插件
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import myUI from './components'
createApp(App).use(store).use(router).use(myUI).mount('#app')
3. 使用演示
將全域組件需要的通過自定義屬性傳遞過去,這里使用固定資料模擬一下,
可以在全域組件外層包裹一個div元素,控制輪播圖組件的大小
- duration :若不傳,默認自動輪播時間為3s
- autoPlay:若不傳,默認不自動輪播
代碼如下(示例):
<template>
<div class="home-banner">
<carousel :list="list" :duration="1500" :autoPlay="true"/>
</div>
</template>
<script>
export default {
name: 'App',
setup() {
const list = [
{ id: 1, imgUrl: 'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fcloud.jpeg' },
{ id: 2, imgUrl: 'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fground.jpeg' },
{ id: 3, imgUrl: 'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fnight.jpeg' },
{ id: 4, imgUrl: 'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fstreet.jpeg' },
{ id: 5, imgUrl: 'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fsun.jpeg' }
]
return { list }
}
}
</script>
<style lang="less" scoped>
.home-banner {
margin: 0 auto;
width: 960px;
height: 540px;
}
</style>
效果圖(如下):

總結
自動輪播、懸停停止、點擊切換的效果均已實作,各位小伙伴喜歡的話可以復制代碼本地看看效果哦~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/300760.html
標籤:其他
上一篇:函式作用域和立即執行函式
下一篇:?數字陣列+1 演算法
