Vue3封装轮播图组件

具体组件代码:

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

效果图

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Vue2 轮播图组件封装: ```vue <template> <div class="slider"> <div class="slider__wrapper" :style="{ transform: 'translateX(' + translateX + 'px)' }"> <div v-for="(item, index) in items" :key="index" class="slider__item"> <img :src="item.imgUrl" alt=""> </div> </div> <div class="slider__dots"> <span v-for="(item, index) in items" :key="index" :class="{ active: currentIndex === index }" @click="handleDotClick(index)"></span> </div> <button class="slider__btn slider__btn--prev" @click="prev"> <i class="iconfont icon-arrow-left"></i> </button> <button class="slider__btn slider__btn--next" @click="next"> <i class="iconfont icon-arrow-right"></i> </button> </div> </template> <script> export default { props: { items: { type: Array, required: true }, interval: { type: Number, default: 3000 } }, data() { return { currentIndex: 0, translateX: 0, timer: null } }, computed: { wrapperWidth() { return this.items.length * 100 + '%' }, itemWidth() { return 100 / this.items.length + '%' } }, mounted() { this.startAutoPlay() }, beforeDestroy() { this.stopAutoPlay() }, methods: { startAutoPlay() { this.timer = setInterval(() => { this.next() }, this.interval) }, stopAutoPlay() { clearInterval(this.timer) }, prev() { this.currentIndex = (this.currentIndex + this.items.length - 1) % this.items.length this.translateX = -this.currentIndex * this.$el.clientWidth }, next() { this.currentIndex = (this.currentIndex + 1) % this.items.length this.translateX = -this.currentIndex * this.$el.clientWidth }, handleDotClick(index) { this.currentIndex = index this.translateX = -this.currentIndex * this.$el.clientWidth } } } </script> <style> .slider { position: relative; overflow: hidden; } .slider__wrapper { display: flex; transition: transform 0.3s ease-out; } .slider__item { width: 100%; flex-shrink: 0; } .slider__dots { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; } .slider__dots span { width: 10px; height: 10px; border-radius: 50%; background-color: #ccc; margin-right: 5px; cursor: pointer; transition: background-color 0.3s ease-out; } .slider__dots span.active { background-color: #333; } .slider__btn { position: absolute; top: 50%; transform: translateY(-50%); width: 30px; height: 30px; background-color: rgba(0, 0, 0, 0.3); border: none; border-radius: 50%; color: #fff; cursor: pointer; transition: background-color 0.3s ease-out; } .slider__btn:hover { background-color: rgba(0, 0, 0, 0.5); } .slider__btn i { font-size: 16px; line-height: 30px; } .slider__btn--prev { left: 20px; } .slider__btn--next { right: 20px; } </style> ``` 使用时,可在父组件中引入并传入轮播图数据: ```vue <template> <slider :items="items" :interval="3000" /> </template> <script> import Slider from '@/components/Slider.vue' export default { components: { Slider }, data() { return { items: [ { imgUrl: 'https://picsum.photos/800/400?random=1' }, { imgUrl: 'https://picsum.photos/800/400?random=2' }, { imgUrl: 'https://picsum.photos/800/400?random=3' } ] } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值