第一步:封装通用的轮播图组件
<template>
<!-- 轮播图展示 -->
<div class="xtx-carousel" @mouseleave="start" @mouseenter="stop">
<ul class="carousel-body">
<li v-for="(item, i) in imgs" :key="i" class="carousel-item" :class="{ fade: i === index }">
<RouterLink to="/">
<img :src="item.imgUrl" alt="" />
</RouterLink>
</li>
</ul>
<!-- 左右切换箭头 -->
<a href="javascript:;" class="carousel-btn prev" @click="toggle(-1)"><i class="iconfont icon-angle-left"></i></a>
<a href="javascript:;" class="carousel-btn next" @click="toggle(1)"><i class="iconfont icon-angle-right"></i></a>
<div class="carousel-indicator">
<!-- 轮播图小圆点 -->
<span v-for="(item, i) in imgs" :key="i" :class="{ active: i === index }" @click="tog(i)"></span>
</div>
</div>
</template>
<script>
import { ref, watch } from 'vue'
export default {
name: 'XtxCarousel',
props: {
// 父组件传来得轮播图数据
imgs: {
type: Array,
default: () => {
return []
}
},
// 轮播图自动播放属性
autoplay: {
type: Boolean,
default: true
},
// 播放间隔时间
duration: { type: Number, default: 2000 }
},
setup(props) {
// 默认显示图片的索引
const index = ref(0)
let timer = null
const autoplayFn = () => {
clearInterval(timer)
timer = setInterval(() => {
index.value++
if (index.value >= props.imgs.length) {
index.value = 0
}
}, props.duration * 1000)
}
watch(
() => props.imgs,
newVal => {
if (newVal.length > 1 && props.autoplay) {
index.value = 0
autoplayFn()
}
},
{ immediate: true }
)
// 鼠标移出轮播图开始播放
const start = () => {
if (props.imgs.length && props.autoplay) autoplayFn()
}
// 鼠标移入轮播图停止播放
const stop = () => {
if (timer) clearInterval(timer)
}
// 点击右侧小箭头;左侧小箭头切换图片
const toggle = step => {
index.value += step
if (index.value >= props.imgs.length) {
index.value = 0
return
}
if (index.value < 0) {
index.value = props.imgs.length - 1
}
}
// 点击小圆点随机切换图片
const tog = i => {
index.value = i
}
return { index, start, stop, toggle, tog }
}
}
</script>
<style scoped lang="less">
.xtx-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,.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>
第二步:全局注册轮播图组件,在components文件夹index.js文件中
import Lunbotu from './xtx-carousel.vue'
export default {
install (app) {
app.component(Lunbotu.name,Lunbotu )
}
}
第三步:在main.js中,注入自己封装好的组件库
import MyUI from '@/components/'
createApp(App)
.use(store)
.use(router)
.use(MyUI)
.mount('#app')
第四步:在组件中直接使用轮播图组件
<Lunbotu :imgs="imgs" :autoplay="true" :duration="2"></Lunbotu>