当前位置:首页 > VUE

vue实现无缝横滚

2026-02-19 02:15:03VUE

Vue 实现无缝横滚效果

无缝横滚通常用于实现无限循环的横向滚动效果,常见于轮播图或新闻列表。以下是两种常见实现方式:

使用 CSS 动画 + 数据循环

通过 Vue 管理数据列表,结合 CSS 动画实现滚动效果:

vue实现无缝横滚

<template>
  <div class="scroll-container">
    <div class="scroll-content" :style="{ animationDuration: duration + 's' }">
      <div v-for="(item, index) in duplicatedItems" :key="index" class="item">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4'],
      duration: 10
    };
  },
  computed: {
    duplicatedItems() {
      return [...this.items, ...this.items];
    }
  }
};
</script>

<style>
.scroll-container {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.scroll-content {
  display: inline-block;
  animation: scroll linear infinite;
}

@keyframes scroll {
  0% { transform: translateX(0); }
  100% { transform: translateX(-50%); }
}

.item {
  display: inline-block;
  padding: 0 20px;
}
</style>

使用 JavaScript 动态控制

通过 requestAnimationFrame 实现更精确的控制:

<template>
  <div class="scroll-container" ref="container">
    <div class="scroll-content" ref="content">
      <div v-for="(item, index) in duplicatedItems" :key="index" class="item">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4'],
      speed: 1,
      position: 0,
      animationId: null
    };
  },
  computed: {
    duplicatedItems() {
      return [...this.items, ...this.items];
    }
  },
  mounted() {
    this.startAnimation();
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId);
  },
  methods: {
    startAnimation() {
      const containerWidth = this.$refs.container.offsetWidth;
      const contentWidth = this.$refs.content.offsetWidth;

      const animate = () => {
        this.position -= this.speed;

        if (-this.position >= contentWidth / 2) {
          this.position = 0;
        }

        this.$refs.content.style.transform = `translateX(${this.position}px)`;
        this.animationId = requestAnimationFrame(animate);
      };

      animate();
    }
  }
};
</script>

<style>
.scroll-container {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.scroll-content {
  display: inline-block;
}

.item {
  display: inline-block;
  padding: 0 20px;
}
</style>

性能优化建议

  • 使用 will-change 属性提升动画性能

    vue实现无缝横滚

    .scroll-content {
    will-change: transform;
    }
  • 对于大量数据,考虑虚拟滚动技术

  • 适当使用节流减少重绘频率

两种方式各有优势:CSS 动画实现简单但控制不够灵活;JavaScript 实现控制精确但代码量稍多。可根据实际需求选择合适方案。

标签: vue
分享给朋友:

相关文章

vue 实现多选

vue 实现多选

Vue 实现多选的方法 在 Vue 中实现多选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定数组 Vue 的 v-model 指令可以与复选框结合使用,绑定到一个…

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性…

vue element实现

vue element实现

Vue Element UI 实现 Vue Element UI 是基于 Vue.js 的组件库,提供丰富的 UI 组件,适合快速开发企业级中后台产品。以下是一些关键实现方法和步骤: 安装 Vue…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v…