当前位置:首页 > VUE

vue实现向右无缝滚动

2026-02-22 16:02:28VUE

实现无缝向右滚动的思路

无缝滚动通常通过动态调整内容位置或克隆节点实现。核心原理是当内容滚动到尽头时,快速重置位置并继续滚动,利用视觉暂留效应形成无缝效果。

基于CSS动画的实现

使用CSS的@keyframestransform属性实现动画效果,结合Vue的数据绑定控制滚动内容。

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

<script>
export default {
  data() {
    return {
      list: ['内容1', '内容2', '内容3', '内容4'],
      scrollWidth: 0
    }
  },
  computed: {
    doubleList() {
      return [...this.list, ...this.list] // 克隆内容实现无缝衔接
    },
    contentStyle() {
      return {
        animation: `scroll ${this.list.length * 2}s linear infinite`,
        width: `${this.scrollWidth}px`
      }
    }
  },
  mounted() {
    this.calculateWidth()
  },
  methods: {
    calculateWidth() {
      const container = this.$el.querySelector('.scroll-content')
      this.scrollWidth = container.scrollWidth / 2
    }
  }
}
</script>

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

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

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

基于JavaScript定时器的实现

通过定时器动态修改元素的scrollLeft属性实现滚动,适合需要精确控制滚动速度的场景。

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

<script>
export default {
  data() {
    return {
      list: ['内容1', '内容2', '内容3', '内容4'],
      scrollSpeed: 2,
      animationFrame: null
    }
  },
  computed: {
    doubleList() {
      return [...this.list, ...this.list]
    }
  },
  mounted() {
    this.startScroll()
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationFrame)
  },
  methods: {
    startScroll() {
      const container = this.$refs.container
      const content = this.$refs.content
      const totalWidth = content.scrollWidth / 2
      let scrollLeft = 0

      const scroll = () => {
        scrollLeft += this.scrollSpeed
        if (scrollLeft >= totalWidth) {
          scrollLeft = 0
        }
        container.scrollLeft = scrollLeft
        this.animationFrame = requestAnimationFrame(scroll)
      }
      this.animationFrame = requestAnimationFrame(scroll)
    }
  }
}
</script>

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

.scroll-content {
  display: inline-block;
}
</style>

性能优化建议

克隆内容时确保原始内容宽度与容器宽度匹配,避免出现空白间隙。对于大量数据,考虑虚拟滚动技术减少DOM节点数量。

vue实现向右无缝滚动

CSS动画实现性能通常优于JavaScript定时器,但灵活性较低。根据实际需求选择合适方案,移动端设备注意兼容性和性能测试。

标签: vue
分享给朋友:

相关文章

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…