当前位置:首页 > VUE

vue实现无缝播报

2026-01-19 21:29:30VUE

Vue实现无缝播报

无缝播报通常指循环滚动展示信息,常见于新闻、公告等场景。以下是基于Vue的实现方法:

使用CSS动画实现

通过CSS的@keyframestransform实现滚动效果,结合Vue动态绑定数据。

<template>
  <div class="marquee-container">
    <div class="marquee-content" :style="animationStyle">
      {{ content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: '这是一条需要无缝播报的信息',
      duration: 10 // 动画持续时间(秒)
    }
  },
  computed: {
    animationStyle() {
      return {
        animation: `marquee ${this.duration}s linear infinite`
      }
    }
  }
}
</script>

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

.marquee-content {
  display: inline-block;
  padding-left: 100%;
}

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

使用定时器实现

通过JavaScript定时器动态更新内容位置,实现更灵活的控制。

vue实现无缝播报

<template>
  <div class="marquee" ref="marquee">
    <div class="content" :style="{ left: position + 'px' }">
      {{ content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: '这是一条需要无缝播报的信息',
      position: 0,
      speed: 1,
      requestId: null
    }
  },
  mounted() {
    this.startAnimation()
  },
  beforeDestroy() {
    cancelAnimationFrame(this.requestId)
  },
  methods: {
    startAnimation() {
      const animate = () => {
        this.position -= this.speed
        if (Math.abs(this.position) > this.$refs.marquee.offsetWidth) {
          this.position = this.$refs.marquee.offsetWidth
        }
        this.requestId = requestAnimationFrame(animate)
      }
      animate()
    }
  }
}
</script>

<style>
.marquee {
  position: relative;
  overflow: hidden;
  white-space: nowrap;
  height: 20px;
}

.content {
  position: absolute;
}
</style>

使用第三方库

vue-marquee-text-component是一个专门实现跑马灯效果的Vue组件。

安装:

vue实现无缝播报

npm install vue-marquee-text-component

使用:

<template>
  <marquee-text :duration="5" :repeat="Infinity">
    {{ content }}
  </marquee-text>
</template>

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

export default {
  components: {
    MarqueeText
  },
  data() {
    return {
      content: '这是一条需要无缝播报的信息'
    }
  }
}
</script>

多内容循环播报

对于多条信息循环显示的情况,可以结合定时器和过渡效果实现。

<template>
  <div class="news-container">
    <transition name="fade" mode="out-in">
      <div :key="currentIndex" class="news-item">
        {{ newsList[currentIndex] }}
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newsList: [
        '第一条新闻',
        '第二条公告',
        '第三条消息'
      ],
      currentIndex: 0,
      interval: null
    }
  },
  mounted() {
    this.interval = setInterval(() => {
      this.currentIndex = (this.currentIndex + 1) % this.newsList.length
    }, 3000)
  },
  beforeDestroy() {
    clearInterval(this.interval)
  }
}
</script>

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

.news-item {
  position: absolute;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

以上方法可根据实际需求选择,CSS动画性能较好但控制不够灵活,JavaScript实现更可控但需要注意性能优化,第三方库则提供了开箱即用的解决方案。

标签: vue
分享给朋友:

相关文章

vue实现语音

vue实现语音

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

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现布局

vue实现布局

Vue 实现布局方法 Vue 提供了多种方式实现页面布局,可以通过组件化、CSS 框架或自定义样式完成。以下是几种常见方法: 使用 CSS Flexbox 或 Grid 通过 Vue 单文件组件结合…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…

vue 实现vshow

vue 实现vshow

Vue 实现 v-show 在 Vue 中,v-show 是一个常用的指令,用于根据条件控制元素的显示或隐藏。与 v-if 不同,v-show 不会移除 DOM 元素,而是通过 CSS 的 displ…

vue实现双击

vue实现双击

Vue 实现双击事件的方法 在Vue中实现双击事件可以通过以下几种方式完成,具体选择取决于项目需求和开发习惯。 使用 @dblclick 指令 Vue提供了内置的@dblclick指令,可以直接在模…