当前位置:首页 > VUE

vue实现文字向上滚动

2026-02-21 10:51:13VUE

实现文字向上滚动的 Vue 方法

使用 CSS 动画实现基础滚动

通过 CSS @keyframestransform 实现垂直滚动效果,适用于静态内容。

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

<script>
export default {
  data() {
    return {
      items: ['内容1', '内容2', '内容3', '内容4']
    }
  }
}
</script>

<style>
.scroll-container {
  height: 100px;
  overflow: hidden;
  position: relative;
}

.scroll-content {
  animation: scroll 10s linear infinite;
}

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

动态数据无限循环滚动

结合 setInterval 实现动态数据更新,适合实时数据场景。

vue实现文字向上滚动

<template>
  <div class="scroll-box">
    <div 
      class="scroll-list" 
      :style="{ transform: `translateY(${offset}px)` }"
    >
      <div v-for="(item, index) in list" :key="index">{{ item.text }}</div>
      <!-- 复制内容实现无缝衔接 -->
      <div v-for="(item, index) in list" :key="index + '_copy'">{{ item.text }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { text: '动态内容A' },
        { text: '动态内容B' },
        { text: '动态内容C' }
      ],
      offset: 0,
      timer: null
    }
  },
  mounted() {
    this.startScroll()
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    startScroll() {
      const speed = 1 // 滚动速度
      const itemHeight = 30 // 单行高度
      this.timer = setInterval(() => {
        this.offset -= speed
        if (Math.abs(this.offset) >= this.list.length * itemHeight) {
          this.offset = 0
        }
      }, 50)
    }
  }
}
</script>

<style>
.scroll-box {
  height: 90px;
  overflow: hidden;
}
.scroll-list {
  transition: transform 0.3s linear;
}
</style>

使用第三方库 vue-seamless-scroll

对于复杂需求,可直接使用专门开发的第三方组件库。

vue实现文字向上滚动

安装依赖:

npm install vue-seamless-scroll

实现示例:

<template>
  <vue-seamless-scroll 
    :data="list" 
    :class-option="options"
    class="seamless-wrap"
  >
    <ul>
      <li v-for="(item, index) in list" :key="index">{{ item }}</li>
    </ul>
  </vue-seamless-scroll>
</template>

<script>
import vueSeamlessScroll from 'vue-seamless-scroll'
export default {
  components: { vueSeamlessScroll },
  data() {
    return {
      list: ['数据A', '数据B', '数据C', '数据D'],
      options: {
        direction: 2, // 0向下 1向上 2向左 3向右
        limitMoveNum: 3, // 可视区域内移动的元素数量
        step: 1, // 步长
        hoverStop: true // 悬停暂停
      }
    }
  }
}
</script>

<style>
.seamless-wrap {
  height: 120px;
  overflow: hidden;
}
</style>

性能优化建议

  • 对于大量数据采用虚拟滚动技术,只渲染可视区域内容
  • 使用 will-change: transform 提升 CSS 动画性能
  • 动态数据场景下注意清除定时器避免内存泄漏
  • 移动端考虑使用 requestAnimationFrame 替代 setInterval

标签: 文字vue
分享给朋友:

相关文章

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除操…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue实现menu

vue实现menu

Vue 实现 Menu 的方法 使用 Element UI 的 Menu 组件 Element UI 提供了现成的 Menu 组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue实现vr

vue实现vr

Vue 实现 VR 的方法 Vue 可以与 WebVR 或 WebXR API 结合使用,通过 Three.js、A-Frame 等库实现 VR 效果。以下是几种常见方法: 使用 A-Frame 框…