基于 Vue3 封装轮播图组件

一、封装组件

思路:

  1. 通过 fade 类名控制图片的显隐;
  2. 通过定义的响应式变量 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>
// 按需引入 ref 响应数据 | watch 侦听器 | onUnmounted 组件销毁生命周期函数
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 生命周期函数区域
  setup(props) {
    // 定义轮播图的默认索引值
    const index = ref(0)
    // 定义定时器标识符
    const timer = ref(null)

    // 定义轮播图方法
    const autoplayFN = () => {
      // 默认先清除当前定时器标识符以防止定时器重叠
      clearInterval(timer.value)
      // 定义定时器
      timer.value = setInterval(() => {
        // 改变轮播图的默认索引值
        index.value++
        // 当轮播图的默认索引值超出轮播图数据的长度时恢复为默认值 0
        if (index.value >= props.sliders.length) index.value = 0
      }, props.duration * 1000) // 根据父组件传的时间数据设置定时器的间隔时间
    }

    // 侦听 sliders 轮播图数据变化 --> 是否启用轮播图
    watch(
      () => props.sliders,
      () => {
        // 如果父组件所传数据为自动播放并且轮播图大于两张时轮播图开启
        if (props.sliders.length > 1 && props.autoplay) {
          // 轮播开始前重置下标
          index.value = 0
          // 调用轮播图方法
          autoplayFN()
        }
      },
      {
        // 页面创建时就进行侦听
        immediate: true
      }
    )

    // 鼠标悬停至 banner 区域时 --> 轮播图停止 | 继续
    // 停止轮播
    const stopFN = () => {
      // 通过清除定时器来停止轮播
      if (timer.value) clearInterval(timer.value)
    }
    // 继续轮播
    const continueFN = () => {
      // 继续轮播时需判断是否为自动播放并且轮播图大于两张
      if (props.sliders.length > 1 && props.autoplay) autoplayFN()
    }

    // 点击左右按钮跳转轮播图
    const toggleFN = num => {
      // 修改 index
      index.value += num
      // 当轮播图的默认索引值超出轮播图数据的长度时恢复为默认值 0
      if (index.value >= props.sliders.length) return (index.value = 0)
      // 当轮播图的默认索引值小于 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" />

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值