当前位置:首页 > 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定时器动态更新内容位置,实现更灵活的控制。

<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组件。

安装:

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实现文档分享

Vue 实现文档分享功能 文档分享功能通常涉及文件上传、存储、生成分享链接以及权限控制等模块。以下是基于 Vue 的实现方案: 文件上传组件 使用 element-ui 或 ant-design-…

vue实现皮肤切换

vue实现皮肤切换

实现皮肤切换的基本思路 在Vue中实现皮肤切换功能,通常涉及动态修改CSS变量或类名。核心是通过状态管理当前主题,并在组件中应用对应的样式。 使用CSS变量实现主题切换 CSS变量(自定义属性)是…

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue实现微博印象

vue实现微博印象

实现微博印象功能的核心思路 微博印象功能通常指用户发布微博后,系统自动分析文本内容并提取关键词或标签,以可视化形式展示。Vue实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键…

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debo…