vue3实现轮播图组件封装

学习内容:vue3实现轮播图组件封装

一:结构

<template>
  <div class="x-carousel" @mouseenter="stop()" @mouseleave="start()">
    <ul class="carousel-body">
      <li
        class="carousel-item"
        v-for="(item, i) in sliders"
        :key="i"
        :class="{ fade: index === i }"
      >
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a @click="toggle(-1)" href="javascript:;" class="carousel-btn prev"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <a @click="toggle(1)" href="javascript:;" class="carousel-btn next"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <div class="carousel-indicator">
      <span
        @click="index=i"
        v-for="(item, i) in sliders"
        :key="i"
        :class="{ active: index === i }"
      ></span>
    </div>
  </div>
</template>

二: 样式

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

三: 逻辑

  1. 如果页面中有多处使用轮播图,可以将其封装成一个全局组件,通过props控制该轮播图组件的图片数据,是否自动播放,间隔时长。

props:

// 轮播图数据

 sliders: {
   type: Array,
   default: () => [],
 },
 // 是否自动轮播
 autoPlay: {
   type: Boolean,
   default: false,
 },
 // 间隔时长
 duration: {
   type: Number,
  default: 3000,
},
 },
  1. 自动轮播的逻辑

  const index = ref(0);
   var timer = null;
   const auroPlayFn = () => {
     clearInterval(timer);
     timer = setInterval(() => {
       index.value++;
       if (index.value >= props.sliders.length) {
         index.value = 0;
       }
     }, props.duration);
   };
  1. 通过watch监听数据改变后是否自动播放

watch(
 () => props.sliders,
 (newVal) => {
   if (newVal.length && props.autoPlay) {
     auroPlayFn();
   }
},
{ immediate: true }
 	  );
  1. 鼠标进入暂停定时器,离开开启定时器,给父元素绑定 @mouseenter="stop()" @mouseleave="start()"事件

   const stop = () => {
     if (timer) clearInterval(timer);
   };
   const start = () => {
     if (props.sliders.length && props.autoPlay) {
       auroPlayFn();
    }
  };
  1. 点击切换上/下一张

 const toggle = (step) => {
      const newIndex = index.value + step;
      if (newIndex > props.sliders.length - 1) {
        index.value = 0;
        return;
      }
      if (newIndex < 0) {
        index.value = props.sliders.length - 1;
        return;
      }
  index.value = newIndex;
    };
  1. 组件卸载时,清除定时器

 onUnmounted(() => {
     clearInterval(timer);
   });

心得:

掌握了将轮播图组件封装成全局组件,当页面多个地方需要时,可以用一个组件实现,通过组件间通信实现不同轮播图之间数据的渲染

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于Vue.js实现轮播图组件: ```html <template> <div class="slider"> <div class="slider-wrapper" :style="{ transform: 'translateX(-' + currentImage * 100 + '%)' }"> <div class="slider-item" v-for="(image, index) in images" :key="index" :style="{ backgroundImage: 'url(' + image + ')' }"> </div> </div> <div class="slider-dots"> <span class="dot" v-for="(image, index) in images" :key="index" :class="{ active: currentImage === index }" @click="goToImage(index)"> </span> </div> </div> </template> <script> export default { data() { return { currentImage: 0, interval: null, }; }, props: { images: { type: Array, required: true, }, intervalTime: { type: Number, default: 3000, }, }, methods: { startSlider() { this.interval = setInterval(() => { this.currentImage++; if (this.currentImage >= this.images.length) { this.currentImage = 0; } }, this.intervalTime); }, goToImage(index) { this.currentImage = index; clearInterval(this.interval); this.startSlider(); }, }, mounted() { this.startSlider(); }, beforeUnmount() { clearInterval(this.interval); }, }; </script> <style scoped> .slider { position: relative; overflow: hidden; } .slider-wrapper { display: flex; transition: transform 0.5s ease-out; } .slider-item { width: 100%; height: 0; padding-bottom: 56.25%; background-position: center; background-size: cover; } .slider-dots { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); display: flex; } .slider-dots .dot { width: 10px; height: 10px; border-radius: 50%; background-color: #ccc; margin-right: 10px; cursor: pointer; } .slider-dots .active { background-color: #333; } </style> ``` 这个轮播图组件接受一个`images`数组作为参数,每个元素是一个图片的URL。可以通过设置`intervalTime`属性来控制自动播放的时间间隔,默认为3秒。组件内部使用了`setInterval`函数来实现自动播放,并且在组件销毁前使用`clearInterval`函数清除定时器。底部的小圆点会根据当前显示的图片来更新样式,并且可以点击切换到对应的图片。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值