当前位置:首页 > VUE

vue实现文字自动滚动

2026-01-23 03:25:28VUE

实现文字自动滚动的Vue方法

使用CSS动画和Vue结合实现文字自动滚动效果,适用于公告栏、跑马灯等场景。

基础CSS动画实现

vue实现文字自动滚动

<template>
  <div class="scroll-container">
    <div class="scroll-text" :style="{ animationDuration: duration + 's' }">
      {{ text }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    text: String,
    duration: {
      type: Number,
      default: 10
    }
  }
}
</script>

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

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

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

使用requestAnimationFrame实现更精确控制

<template>
  <div ref="container" class="scroll-container">
    <div ref="content" class="scroll-content">
      {{ text }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    text: String,
    speed: {
      type: Number,
      default: 50
    }
  },
  mounted() {
    this.startScrolling()
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
  },
  methods: {
    startScrolling() {
      const container = this.$refs.container
      const content = this.$refs.content

      let position = container.offsetWidth

      const animate = () => {
        position -= 1
        if (position < -content.offsetWidth) {
          position = container.offsetWidth
        }

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

      animate()
    }
  }
}
</script>

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

.scroll-content {
  display: inline-block;
  position: absolute;
  white-space: nowrap;
}
</style>

双向滚动实现(来回滚动)

vue实现文字自动滚动

<template>
  <div class="scroll-container">
    <div 
      class="scroll-text" 
      :style="{ 
        animationDuration: duration + 's',
        animationDirection: direction 
      }"
    >
      {{ text }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    text: String,
    duration: {
      type: Number,
      default: 5
    },
    direction: {
      type: String,
      default: 'alternate',
      validator: value => ['normal', 'reverse', 'alternate', 'alternate-reverse'].includes(value)
    }
  }
}
</script>

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

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

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

多行文本垂直滚动

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

<script>
export default {
  props: {
    items: Array,
    duration: {
      type: Number,
      default: 10
    }
  },
  mounted() {
    const content = this.$el.querySelector('.vertical-scroll-content')
    content.style.animationDuration = `${this.duration}s`
  }
}
</script>

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

.vertical-scroll-content {
  position: absolute;
  animation: vertical-scroll linear infinite;
}

@keyframes vertical-scroll {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-100%);
  }
}

.scroll-item {
  padding: 8px 0;
}
</style>

注意事项

  1. 性能优化:对于大量文本或复杂动画,使用CSS动画通常比JavaScript性能更好
  2. 响应式设计:确保容器宽度适应不同屏幕尺寸
  3. 循环处理:长文本需要考虑无缝循环效果
  4. 暂停控制:可通过添加暂停功能提升用户体验
  5. 无障碍访问:确保滚动内容可被屏幕阅读器识别

以上方法可根据实际需求组合或调整参数,如滚动速度、方向、触发条件等。

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

相关文章

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现帖子

vue实现帖子

Vue 实现帖子功能 使用 Vue 实现帖子功能需要结合前端框架和后端数据交互,以下是实现的关键步骤和代码示例。 帖子列表展示 创建帖子列表组件,使用 v-for 渲染帖子数据,通过 axios 获…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue搜索实现

vue搜索实现

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

vue实现xterm

vue实现xterm

在 Vue 中集成 Xterm.js Xterm.js 是一个基于 TypeScript 的前端终端组件库,可用于在浏览器中实现终端功能。以下是在 Vue 项目中集成 Xterm.js 的详细步骤。…

mvvm实现vue

mvvm实现vue

MVVM 模式在 Vue 中的实现 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 的设计灵感来源于此。以下是 MVVM 在 Vue 中的核心实现方式: 数据绑定(M…