vue3的element-plus在外部自定义指示器切换轮播图(走马灯)

<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>

相关推荐

  1. android入门2——触摸停止与指示器

    2024-02-02 19:52:01       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-02 19:52:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-02 19:52:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-02 19:52:01       87 阅读
  4. Python语言-面向对象

    2024-02-02 19:52:01       96 阅读

热门阅读

  1. SpringBoot+JdbcTempalte+SQLServer

    2024-02-02 19:52:01       54 阅读
  2. centos7 时区设置 时间同步

    2024-02-02 19:52:01       45 阅读
  3. 基于python的新闻爬虫

    2024-02-02 19:52:01       60 阅读
  4. Vue3+Koa2实现图片上传(不再畏惧)

    2024-02-02 19:52:01       56 阅读
  5. 爬虫的两个小案例

    2024-02-02 19:52:01       46 阅读
  6. 【深度学习】ND4J-科学计算库

    2024-02-02 19:52:01       52 阅读
  7. 轻松使用python将PDF转换为图片(成功)

    2024-02-02 19:52:01       50 阅读
  8. 考研英语单词20

    2024-02-02 19:52:01       51 阅读
  9. 响应标头Allow-Headers和Expose-Headers的区别和用法

    2024-02-02 19:52:01       50 阅读