当前位置:首页 > VUE

vue实现文字播放栏

2026-01-07 02:47:42VUE

Vue 实现文字播放栏(跑马灯效果)

方法一:使用 CSS 动画 + Vue 数据绑定

通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。

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

<script>
export default {
  props: {
    text: {
      type: String,
      default: '这是滚动文字内容'
    },
    duration: {
      type: Number,
      default: 10
    }
  }
}
</script>

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

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

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

方法二:纯 Vue 实现(动态计算位置)

通过 Vue 的响应式数据和计算属性动态计算位置,实现更灵活的控制。

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

<script>
export default {
  data() {
    return {
      position: 0,
      containerWidth: 0,
      textWidth: 0,
      speed: 2,
      animationId: null
    }
  },
  props: {
    text: String
  },
  mounted() {
    this.initSizes()
    this.startAnimation()
    window.addEventListener('resize', this.initSizes)
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
    window.removeEventListener('resize', this.initSizes)
  },
  methods: {
    initSizes() {
      this.containerWidth = this.$refs.container.offsetWidth
      const tempSpan = document.createElement('span')
      tempSpan.style.visibility = 'hidden'
      tempSpan.style.whiteSpace = 'nowrap'
      tempSpan.innerText = this.text
      document.body.appendChild(tempSpan)
      this.textWidth = tempSpan.offsetWidth
      document.body.removeChild(tempSpan)
    },
    startAnimation() {
      const animate = () => {
        this.position -= this.speed
        if (this.position < -this.textWidth) {
          this.position = this.containerWidth
        }
        this.animationId = requestAnimationFrame(animate)
      }
      animate()
    }
  }
}
</script>

<style>
.marquee {
  width: 100%;
  overflow: hidden;
  position: relative;
  height: 1.2em;
}

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

方法三:使用第三方库(vue-marquee-text-component)

vue实现文字播放栏

安装 marquee 组件库可以快速实现:

npm install vue-marquee-text-component

使用示例:

<template>
  <marquee-text :duration="5" :repeat="3">
    这是要滚动的文字内容
  </marquee-text>
</template>

<script>
import MarqueeText from 'vue-marquee-text-component'

export default {
  components: {
    MarqueeText
  }
}
</script>

自定义功能扩展

vue实现文字播放栏

  1. 添加暂停/继续功能:

    // 在方法二中增加
    methods: {
    toggleMarquee() {
     if (this.animationId) {
       cancelAnimationFrame(this.animationId)
       this.animationId = null
     } else {
       this.startAnimation()
     }
    }
    }
  2. 支持多行文字轮播:

    
    <template>
    <div class="multi-marquee">
     <transition-group name="marquee" tag="div">
       <div v-for="(item, index) in items" :key="index">
         {{ item }}
       </div>
     </transition-group>
    </div>
    </template>
export default { data() { return { items: ['第一条消息', '第二条消息', '第三条消息'], currentIndex: 0 } }, mounted() { setInterval(() => { this.currentIndex = (this.currentIndex + 1) % this.items.length }, 3000) } } .marquee-enter-active, .marquee-leave-active { transition: all 0.5s; } .marquee-enter, .marquee-leave-to { opacity: 0; transform: translateY(20px); } ```

注意事项

  1. 性能优化:对于长文本或高频更新,建议使用 CSS 动画而非 JavaScript 计算
  2. 响应式处理:在窗口大小变化时重新计算容器和文本宽度
  3. 移动端适配:适当调整动画速度和持续时间
  4. 无障碍访问:为动态内容添加 ARIA 属性

以上方案可根据实际需求选择,CSS 方案性能最佳,纯 Vue 方案灵活性最强,第三方库方案最便捷。

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

相关文章

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动…