当前位置:首页 > VUE

vue 实现跑马灯

2026-01-21 01:36:39VUE

Vue 实现跑马灯效果

跑马灯效果通常指文字或内容水平滚动循环展示。以下是两种常见的实现方式:

使用 CSS 动画

通过 CSS 的 animation@keyframes 实现滚动效果:

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

<script>
export default {
  data() {
    return {
      text: '这里是需要滚动的文字内容,可以自定义长度和内容'
    }
  }
}
</script>

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

.marquee-content {
  display: inline-block;
  animation: marquee 10s linear infinite;
}

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

使用 JavaScript 控制

通过定时器动态修改样式实现更灵活的控制:

<template>
  <div class="marquee-wrapper" ref="marquee">
    <div class="marquee-text" :style="{ transform: `translateX(${offset}px)` }">
      {{ text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '可自定义的滚动内容',
      offset: 0,
      timer: null,
      speed: 2
    }
  },
  mounted() {
    this.startMarquee()
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    startMarquee() {
      const containerWidth = this.$refs.marquee.offsetWidth
      const textWidth = this.$refs.marquee.querySelector('.marquee-text').offsetWidth

      this.timer = setInterval(() => {
        this.offset -= this.speed
        if (this.offset < -textWidth) {
          this.offset = containerWidth
        }
      }, 20)
    }
  }
}
</script>

<style>
.marquee-wrapper {
  width: 100%;
  overflow: hidden;
  position: relative;
  height: 30px;
}

.marquee-text {
  position: absolute;
  white-space: nowrap;
}
</style>

双向滚动支持

添加方向控制参数实现左右滚动:

props: {
  direction: {
    type: String,
    default: 'left', // 'left' 或 'right'
    validator: value => ['left', 'right'].includes(value)
  }
},
methods: {
  startMarquee() {
    // ...
    this.timer = setInterval(() => {
      this.offset += this.direction === 'left' ? -this.speed : this.speed
      // ...
    }, 20)
  }
}

性能优化建议

对于频繁更新的动画,使用 requestAnimationFrame 替代 setInterval

vue 实现跑马灯

methods: {
  startMarquee() {
    const step = () => {
      this.offset -= this.speed
      if (this.offset < -textWidth) {
        this.offset = containerWidth
      }
      this.rafId = requestAnimationFrame(step)
    }
    this.rafId = requestAnimationFrame(step)
  },
  beforeDestroy() {
    cancelAnimationFrame(this.rafId)
  }
}

以上方法可根据实际需求调整速度、方向和内容。CSS 方案简单但灵活性有限,JavaScript 方案可控性更强但代码量稍多。

标签: 跑马灯vue
分享给朋友:

相关文章

实现 vue ssr

实现 vue ssr

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

vue实现语音

vue实现语音

Vue 实现语音功能的方法 使用 Web Speech API Vue 可以结合 Web Speech API 实现语音识别和合成功能。Web Speech API 包含 SpeechRecognit…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…