一、封装组件
思路:
- 通过
fade
类名控制图片的显隐; - 通过定义的响应式变量
index
控制当前显示的图片;
<template>
<div class="carousel" @mouseenter="stopFN" @mouseleave="continueFN">
<ul class="carousel-body">
<!-- 轮播的图片 -->
<li v-for="(item, i) in sliders" :key="item.id" class="carousel-item" :class="{ fade: i === index }">
<RouterLink to="/">
<img :src="item.imgUrl" alt="" />
</RouterLink>
</li>
</ul>
<!-- 左侧上一页点击按钮 -->
<a @click="toggleFN(-1)" href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
<!-- 右侧下一页点击按钮 -->
<a @click="toggleFN(1)" href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
<!-- 导航条区域 -->
<div class="carousel-indicator">
<span v-for="i in sliders.length" :key="i" :class="{ active: index === i - 1 }" @click="index = i - 1"></span>
</div>
</div>
</template>
<script>
import { ref, watch, onUnmounted } from 'vue'
export default {
name: 'Carousel',
props: {
sliders: {
type: Array,
default: () => []
},
duration: {
type: Number,
default: 1
},
autoplay: {
type: Boolean,
default: false
}
},
setup(props) {
const index = ref(0)
const timer = ref(null)
const autoplayFN = () => {
clearInterval(timer.value)
timer.value = setInterval(() => {
index.value++
if (index.value >= props.sliders.length) index.value = 0
}, props.duration * 1000)
}
watch(
() => props.sliders,
() => {
if (props.sliders.length > 1 && props.autoplay) {
index.value = 0
autoplayFN()
}
},
{
immediate: true
}
)
const stopFN = () => {
if (timer.value) clearInterval(timer.value)
}
const continueFN = () => {
if (props.sliders.length > 1 && props.autoplay) autoplayFN()
}
const toggleFN = num => {
index.value += num
if (index.value >= props.sliders.length) return (index.value = 0)
if (index.value < 0) index.value = props.sliders.length - 1
}
onUnmounted(() => {
if (timer.value) clearInterval(timer.value)
})
return { index, stopFN, continueFN, toggleFN }
}
}
</script>
<style scoped lang="less">
.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 XtxCarousel from './xtx-carousel.vue'
export default {
install(app) {
app.component(Carousel.name, Carousel)
}
}
三、在 main.js 入口文件中引入
import Carousel from '@/components/library/index.js'
createApp(App)
.use(store)
.use(router)
.use(Carousel)
.mount('#app')
四、在 vue 组件中使用
<XtxCarousel :sliders="sliders" :duration="1.8" :autoplay="true" />