具体组件代码:

<template>
  <div class="xtx-carousel" @mouseleave="start" @mouseenter="stop">
    <!-- 轮播图列表 -->
    <ul class="carousel-body">
      <li class="carousel-item " v-for="(item, i) in sliders" :key="i" :class="{ fade: index === i }">
        <!-- {{ item.imgUrl }} -->
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <!-- 指示器 左右两边的箭头 -->
    <a href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left" @click="toggle(-1)"></i></a>
    <a href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right" @click="toggle(1)"></i></a>
    <!-- 录播图中间的点 -->
    <div class="carousel-indicator">
      <span v-for="(item, i) in sliders" :key="i" :class="{ active: index === i }" @click="togglepig(i)"></span>
    </div>
  </div>
</template>

<script>
import { ref, watch, onUnmounted } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 父组件传过来的轮播图 图片数据
    sliders: {
      type: Array,
      default: () => []
    },
    // 轮播的时长 单位毫秒
    duration: {
      type: Number,
      default: 2000
    },
    // 是否启动轮播
    autoPlay: {
      type: Boolean,
      default: true
    }
  },
  setup(props) {
    // 控制当前选中的轮播图片
    const index = ref(0)
    const timer = ref(null)
    // 轮播图启动的定时器
    const autoPlayFn = () => {
      // 防止启动多个定时器
      clearInterval(timer.value)
      timer.value = setInterval(() => {
        index.value += 1
        if (index.value >= props.sliders.length) {
          // 超出图片显示的范围从头开始
          index.value = 0
        }
      }, props.duration)
    }
    // 监听父组件传过来的有没有数据
    watch(
      () => props.sliders,
      () => {
        // 判断图片大于一张 并且有数据才调用自动轮播函数
        if (props.sliders.length > 1 && props.autoPlay) {
          autoPlayFn()
        }
      },
      { inmediate: true }
    )
    // 鼠标进入的时候关闭轮播图
    const stop = () => {
      if (timer.value) clearInterval(timer.value)
    }
    // 鼠标离开轮播图的时候 重新启动定时器
    const start = () => {
      // 输入函数体
      if (props.sliders.length && props.autoPlay) autoPlayFn()
    }
    // 手动切换轮播图上一张下一张指示器 处理函数
    const toggle = step => {
      // 输入函数体
      index.value += step
      if (index.value >= props.sliders.length) {
        // 超出范围从左侧重新开始
        index.value = 0
      }
      if (index.value < 0) {
        // 超出范围从右侧重新开始
        index.value = props.sliders.length - 1
      }
    }
    // 点击小点的时候切换轮播图
    const togglepig = i => {
      // 输入函数体
      index.value = i
    }
    // 如果切换到其他界面时 清理定时器
    onUnmounted(() => {
      clearInterval(timer.value)
    })
    return { index, stop, start, toggle, togglepig }
  }
}
</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, 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>

父组件中使用

<XtxCarousel :sliders="sliders" :duration="3000" :autoPlay="true" />
<script>
import { findBanner } from '@/api/home'
import { ref } from 'vue'
export default {
  name: 'HomeBanner',
  setup() {
    //sliders是轮播图要使用的图片数据  一般情况下是从后端请求回来的数据
    const sliders = ref([])
    findBanner()
      .then(res => {
        sliders.value = res.result
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
    return { sliders }
  }
}
</script>

效果图

在这里插入图片描述

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐