vue,原生轮播图(无缝轮播图)

本文介绍了一个使用 Vue.js 编写的无限轮播图组件。代码中实现了初始化克隆元素以解决视觉差,以及上下翻页功能,同时应用了节流技术优化滚动性能。样式设计使得每个轮播项有不同的背景颜色,并且在切换时有平滑的过渡效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

在这里插入图片描述


<template>
  <div>
    <div class="swiper">
      <ul ref="swiper" class="swipero">
        <li v-for="(item, index) in items" :key="index">
          <div v-text="item.name"></div>
        </li>
      </ul>
    </div>
    <div @click="preNext()">下一页</div>
    <div @click="preLast()">上一页</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: "1" },
        { id: 1, name: "2" },
        { id: 1, name: "3" },
      ],
      timer: null,
      active: 0,
      flag: true,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化克隆一个元素,用来解决视觉差的效果(瞬移)
    init() {
      let swiper = this.$refs.swiper;
      let first = swiper.firstElementChild.cloneNode(true);
      swiper.appendChild(first);
    },
    //下一页
    next() {
      let swiper = this.$refs.swiper;
      // 判断是否是最后一个
      // debugger;
      if (this.$refs.swiper.children.length - 2 <= this.active) {
        // debugger
        setTimeout(() => {
          let swiper = this.$refs.swiper;
          this.active = 0;

          swiper.style.transition = "none";
          swiper.style.left = -200 * this.active + "px";
        }, 500);
      }
      this.active++;
      swiper.style.transition = "all .5s";
      swiper.style.left = -200 * this.active + "px";
    },
    // 上一页
    pre() {
      let swiper = this.$refs.swiper;
      // 判断是否是第一个,线瞬间移动到最后,然后再实现动画效果
      if (this.active == 0) {
        let len = this.$refs.swiper.children.length;
        this.active = len - 1;
        // debugger
        swiper.style.transition = "none";
        swiper.style.left = -200 * this.active + "px";
        setTimeout(() => {
          this.active = len - 2;
          swiper.style.transition = "all .5s";
          swiper.style.left = -200 * this.active + "px";
        }, 300);
      } else {
        this.active--;
        swiper.style.transition = "all .5s";
        swiper.style.left = -200 * this.active + "px";

        // this.swiper();
      }
    },
    // 节流
    throttle(callback,speed=3000) {
      let self = this;
      if (self.flag) {
        clearTimeout(this.timer);
        self.timer = setTimeout(() => {
          callback();
          self.flag = true;
        }, speed);
      }
      this.flag = false;
    },
    // 下一页(节流)
    preNext() {
      this.throttle(this.next,1000);
    },
    // 上一页(节流)
    preLast() {
      this.throttle(this.pre,1000);
    },
    // 自动轮播
    swiper() {
      let self = this;
      setInterval(() => {
        self.pre();
      }, 3000);
    },
  },
};
</script>

<style lang="less" scoped>
.swiper {
  width: 200px;
  height: 200px;
  overflow: hidden;
  position: relative;
  ul {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100vw;
    white-space: nowrap;
    li {
      width: 200px;
      height: 200px;
      display: inline-block;
      vertical-align: bottom;
      position: relative;
      // margin-right: 20px;

      transition: all 0.5s;

      > div {
        width: 100%;
        height: 100%;
      }
      &:nth-child(1) {
        background-color: aquamarine;
      }
      &:nth-child(2) {
        background-color: rgb(255, 204, 127);
      }
      &:nth-child(3) {
        background-color: rgb(255, 127, 144);
      }
      &:nth-child(4) {
        background-color: rgb(127, 255, 223);
      }
    }
    .active {
      width: 400px;
      height: 400px;
    }
  }
}
</style>


### Vue 实现无缝切换轮播图 为了实现在 Vue 中的无缝切换轮播图,可以采用原生 JavaScript 结合 CSS 动画的方式。这种方式不仅能够兼容 Vue 3 版本,而且性能更佳[^1]。 #### 创建基础结构 首先定义 HTML 和组件模板部分: ```html <template> <div class="carousel"> <ul :style="{ transform: `translateX(${offset}px)` }" ref="list"> <li v-for="(item, index) in items.concat(items)" :key="index">{{ item }}</li> </ul> </div> </template> <script setup> import { ref, onMounted, watchEffect } from 'vue' const props = defineProps({ items: { type: Array, required: true } }) let offset = ref(0) const list = ref(null) onMounted(() => { setInterval(autoScroll, 2000); }); function autoScroll() { const firstItemWidth = list.value.children[0].clientWidth; if (Math.abs(offset.value) >= props.items.length * firstItemWidth) { offset.value = 0; // 当滚动到最后一项时重置位置 } offset.value -= firstItemWidth; } </script> <style scoped> .carousel ul { display: flex; transition: all .5s ease-in-out; } .carousel li { min-width: 100px; text-align: center; line-height: 100px; border-right: solid 1px #ccc; } </style> ``` 此代码片段创建了一个简单的无限循环轮播图,在每次定时器触发时自动向左移动一项宽度的距离,并在到达最后一个项目后重新回到起始位置。 通过上述方法可以在 Vue 应用程序中轻松实现一个具有无缝切换效果的轮播图组件。这种方法利用了现代浏览器对于 CSS 变换的支持以及高效的硬件加速特性来提供流畅体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值