当前位置:首页 > 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)

安装 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>

自定义功能扩展

  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 实现文档导入的方法 在 Vue 中实现文档导入功能通常涉及文件上传、解析和处理。以下是几种常见的方法: 使用原生文件输入和 FileReader 通过 HTML 的原生 <input…

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件…

vue前端实现注册

vue前端实现注册

使用 Vue 实现用户注册功能 环境准备 确保已安装 Vue CLI 或通过 CDN 引入 Vue。若使用 Vue CLI,需安装 axios 或其他 HTTP 库用于后端通信。 注册表单组件 创建…

vue怎么实现动画

vue怎么实现动画

Vue 动画实现方法 Vue 提供了多种方式实现动画效果,包括内置过渡系统、第三方库集成以及 CSS 动画。以下是常见实现方法: 使用 Vue 过渡系统 通过 <transition>…

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue实现菜单调用

vue实现菜单调用

Vue 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const rout…