当前位置:首页 > VUE

vue弹幕实现暂停

2026-02-10 19:14:16VUE

实现Vue弹幕暂停功能的方法

使用CSS动画控制

通过动态切换CSS的animation-play-state属性实现暂停效果。弹幕元素默认设置为animation-play-state: running,暂停时改为paused

vue弹幕实现暂停

<template>
  <div class="danmu-container">
    <div 
      v-for="(item, index) in danmus" 
      :key="index"
      class="danmu-item"
      :style="{ 
        'animation-play-state': isPaused ? 'paused' : 'running',
        'animation-duration': `${item.speed}s`
      }"
    >
      {{ item.text }}
    </div>
    <button @click="togglePause">{{ isPaused ? '播放' : '暂停' }}</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isPaused: false,
      danmus: [
        { text: '弹幕1', speed: 5 },
        { text: '弹幕2', speed: 8 }
      ]
    }
  },
  methods: {
    togglePause() {
      this.isPaused = !this.isPaused
    }
  }
}
</script>

<style>
.danmu-item {
  position: absolute;
  white-space: nowrap;
  animation: move linear;
  animation-fill-mode: forwards;
}

@keyframes move {
  from { transform: translateX(100%); }
  to { transform: translateX(-100%); }
}
</style>

使用JavaScript定时器控制

对于通过JavaScript定时器实现的弹幕,可以清除定时器实现暂停,重新创建定时器实现继续播放。

vue弹幕实现暂停

<template>
  <div class="danmu-container" ref="container">
    <div v-for="(item, index) in visibleDanmus" :key="index" class="danmu-item">
      {{ item.text }}
    </div>
    <button @click="togglePause">{{ isPaused ? '播放' : '暂停' }}</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isPaused: false,
      danmus: ['弹幕1', '弹幕2', '弹幕3'],
      visibleDanmus: [],
      timer: null,
      interval: 1000
    }
  },
  mounted() {
    this.startDanmu()
  },
  methods: {
    startDanmu() {
      this.timer = setInterval(() => {
        if (!this.isPaused) {
          this.visibleDanmus.push({
            text: this.danmus[Math.floor(Math.random() * this.danmus.length)],
            top: Math.random() * 80 + '%'
          })
        }
      }, this.interval)
    },
    togglePause() {
      this.isPaused = !this.isPaused
      if (!this.isPaused && !this.timer) {
        this.startDanmu()
      }
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

使用第三方库实现

对于使用如rc-bullets等第三方弹幕库的情况,通常库会提供暂停API:

import { BulletScreen } from 'rc-bullets'

export default {
  data() {
    return {
      screen: null,
      isPaused: false
    }
  },
  mounted() {
    this.screen = new BulletScreen('.danmu-container', { duration: 5000 })
    this.screen.push('示例弹幕')
  },
  methods: {
    togglePause() {
      this.isPaused ? this.screen.resume() : this.screen.pause()
      this.isPaused = !this.isPaused
    }
  }
}

注意事项

  • CSS动画方式性能较好但控制粒度较粗
  • JavaScript定时器方式控制更灵活但需注意内存管理
  • 暂停时应保留弹幕的当前位置状态
  • 考虑弹幕重叠时的显示层级问题
  • 移动端需额外处理触摸事件暂停逻辑

标签: 弹幕vue
分享给朋友:

相关文章

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> <…

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端运…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…

vue遮罩实现

vue遮罩实现

实现 Vue 遮罩的常见方法 使用 CSS 定位和透明度 创建一个全屏遮罩层,通过 CSS 固定定位覆盖整个视口,并设置半透明背景色。 <template> <div cl…