<template>
<div class="carousel-container">
<p style="text-align: center">----- 自定义指示器 -----</p>
<div class="custom-indicator">
<div
v-for="(item, index) in 4"
:key="index"
:class="{ active: currentIndex === index }"
@click="changeIndicator(index)"
></div>
</div>
<p style="text-align: center">----- 内容 -----</p>
<el-carousel
ref="carousel"
v-model="currentIndex"
:initial-index="currentIndex"
:interval="10000"
indicator-position="none"
arrow="never"
@change="changeHandle"
>
<el-carousel-item v-for="(item, index) in 4" :key="index">
内容 {{ item }}
</el-carousel-item>
</el-carousel>
</div>
</template>
<script setup>
import { ref } from 'vue'
const carousel = ref()
const currentIndex = ref(0)
// 轮播图自动切换,赋值为自定义指示器
const changeHandle = (e) => {
currentIndex.value = e
}
// 通过自定义指示器切换,赋值给轮播图
const changeIndicator = (index) => {
carousel.value.setActiveItem(index)
currentIndex.value = index
}
</script>
<style>
.carousel-container {
position: relative;
}
.custom-indicator {
display: flex;
justify-content: center;
margin-top: 10px;
}
.el-carousel__item h3 {
color: #475669;
opacity: 0.75;
line-height: 150px;
margin: 0;
text-align: center;
}
.el-carousel__item:nth-child(2n) {
background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n + 1) {
background-color: #d3dce6;
}
.custom-indicator div {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #ccc;
margin: 0 5px;
cursor: pointer;
}
.custom-indicator div.active {
background-color: #333;
}
</style>
vue3的element-plus在外部自定义指示器切换轮播图(走马灯)
最新推荐文章于 2024-09-06 13:55:54 发布