vue实现轮播图组件

html部分:

<template>
  <div
    class="SliderContainer"
    ref="slider"
    @touchstart="touchStart"
    @touchend="touchEnd"
    @touchmove="touchMove"
  >
    <div class="sliderGroup" ref="sliderGroup">
      <div class="slider" v-for="item in banners" :key="item.id">
        <a href=""><img :src="item.imgurl" /></a>
      </div>
    </div>
    <div class="dots-wrapper">
      <span
        class="dot"
        v-for="(item, index) in banners"
        :class="{ active: currentPageIndex == index }"
        :key="item.id"
      ></span>
    </div>
  </div>
</template>

css部分:


<style lang="scss" scoped>
.SliderContainer {
  overflow: hidden;
  position: relative;
  height: 180px;
}
.sliderGroup {
  display: flex;
  height: 100%;
}
.sliderGroup .slider {
  flex-shrink: 0;
  a,
  img {
    width: 100%;
    height: 100%;
  }
}
.dots-wrapper {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 8px;
  width: 33%;
  display: flex;
  justify-content: center;
}
.dots-wrapper .dot {
  width: 8px;
  height: 8px;
  border-radius: 8px;
  background-color: #fff;
  opacity: 0.7;
  margin-left: 10px;
}
.dots-wrapper .active {
  width: 12px;
  border-radius: 15px;
  background-color: #f0031f;
}
</style>

js部分:

<script>
export default {
  props: {
    banners: [],
  },
  data() {
    return {
      currentPageIndex: 0, // 当前图片位置
      bannerWith: 0, // banner宽度
      startPoint: 0, // 触摸开始的位置
      endPoint: 0, // 触摸结束的位置
      distance: 0, // 触摸距离
      autoPlay: true, // 是否自动播放
      interval: 2000, // 播放时间间隔
      timer: null, // 定时器
    };
  },
  methods: {
    startPlay() {
      clearInterval(this.timer);
      this.timer = setInterval(() => {
        if (this.currentPageIndex == this.banners.length - 1) {
          this.currentPageIndex = 0;
        } else {
          this.currentPageIndex++;
        }
        const slider = document.getElementsByClassName("slider");
        this.bannerWith = slider[0].offsetWidth;
        slider[0].style.marginLeft =
          -this.bannerWith * this.currentPageIndex + "px";
        slider[0].style.transition = "all in ease";
      }, 2000);
    },
    /**
     * 触摸开始:记录开始位置,停止计时器
     * 触摸移动:记录当前位置,让图片跟着滑动位置移动
     * 触摸结束:判断滑动方向翻页,开启定时器
     */
    touchStart(evt) {
      clearInterval(this.timer);
      this.startPoint = evt.changedTouches[0].pageX;
    },
    touchMove(evt) {
      this.endPoint = evt.changedTouches[0].pageX;
      this.distance = this.startPoint - this.endPoint; 
      if (this.distance == 0) return;
      const slider = document.getElementsByClassName("slider");
      if (
        (this.distance > 0 &&
          this.currentPageIndex !== this.banners.length - 1) ||
        (this.distance < 0 && this.currentPageIndex != 0)
      ) {
        slider[0].style.marginLeft =
          -this.distance - this.bannerWith * this.currentPageIndex + "px";
      }
    },
    touchEnd() {
      const slider = document.getElementsByClassName("slider");
      this.bannerWith = slider[0].offsetWidth
      if (
        this.distance > 0 &&
        this.currentPageIndex !== this.banners.length - 1
      ) {
        if (this.distance > this.bannerWith * 0.5) {
          this.currentPageIndex++;
        }
        slider[0].style.marginLeft =
          -this.currentPageIndex * this.bannerWith + "px";
      } else if (this.distance < 0 && this.currentPageIndex != 0) {
        if (-this.distance > this.bannerWith * 0.3) {
          this.currentPageIndex--;
        }
        slider[0].style.marginLeft =
          - this.bannerWith * this.currentPageIndex + "px";
      }
      console.log(`curIndx:${this.currentPageIndex}`)
      console.log(`marginLeft:${slider[0].style.marginLeft}`)
      this.startPlay();
    },
  },
  mounted() {
    this.startPlay();
  },
};
</script>

效果:

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以参考以下步骤来自定义一个 Vue 轮播图组件: 1. 创建一个 Vue 组件,并定义需要的属性。例如,轮播图列表、轮播图宽度、轮播时间间隔等。 ```javascript <template> <div class="carousel"> <div class="carousel-wrapper" :style="{ width: wrapperWidth + 'px' }"> <div class="carousel-content" :style="{ transform: 'translateX(' + translateX + 'px)' }"> <div v-for="(item, index) in list" :key="index" class="carousel-item" :style="{ width: itemWidth + 'px' }"> <img :src="item.src" alt=""> </div> </div> </div> </div> </template> <script> export default { name: 'Carousel', props: { list: { type: Array, default: () => [] }, interval: { type: Number, default: 3000 }, width: { type: Number, default: 600 } }, data() { return { currentIndex: 0, itemWidth: 0, wrapperWidth: 0, translateX: 0, timer: null } }, mounted() { this.init() }, methods: { init() { this.itemWidth = this.width this.wrapperWidth = this.width * this.list.length this.autoPlay() }, autoPlay() { this.timer = setInterval(() => { this.next() }, this.interval) }, next() { this.currentIndex = (this.currentIndex + 1) % this.list.length this.translateX = -this.currentIndex * this.itemWidth } } } </script> ``` 2. 在组件实现轮播图的逻辑。例如,自动播放、手动滑动等。 3. 根据需要添加样式,使组件的 UI 更美观。 4. 在父组件中使用自定义轮播图组件。 ```javascript <template> <div> <carousel :list="list" :interval="3000" :width="600"></carousel> </div> </template> <script> import Carousel from './Carousel.vue' export default { name: 'App', components: { Carousel }, data() { return { list: [ { src: 'https://picsum.photos/600/300?random=1' }, { src: 'https://picsum.photos/600/300?random=2' }, { src: 'https://picsum.photos/600/300?random=3' } ] } } } </script> ``` 以上就是自定义 Vue 轮播图组件的基本步骤。当然,可以根据实际需要进行更多的扩展和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值