首页 前端知识 vue3实现轮播图组件封装

vue3实现轮播图组件封装

2024-07-02 23:07:24 前端知识 前端哥 872 674 我要收藏
学习内容: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);
   });

心得:

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

转载请注明出处或者链接地址:https://www.qianduange.cn//article/13822.html
标签
评论
发布的文章

读魏书生的心得体会

2024-07-03 14:07:10

jQuery 选择器

2024-05-12 00:05:34

Vue中public/assets目录区别

2024-07-02 23:07:29

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!