当前位置:首页 > VUE

vue实现弹幕

2026-01-13 00:36:14VUE

Vue 实现弹幕功能

弹幕功能通常包括文字在屏幕上滚动显示的效果。以下是使用 Vue 实现弹幕的几种方法:

使用 CSS 动画实现基础弹幕

创建一个 Vue 组件,利用 CSS 的 @keyframes 实现弹幕的滚动效果。

vue实现弹幕

<template>
  <div class="danmu-container">
    <div 
      v-for="(item, index) in danmuList" 
      :key="index" 
      class="danmu-item"
      :style="{ top: `${item.top}px`, animationDuration: `${item.duration}s` }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      danmuList: [],
      containerHeight: 300 // 容器高度
    };
  },
  methods: {
    addDanmu(text) {
      const top = Math.random() * this.containerHeight;
      const duration = 5 + Math.random() * 5; // 随机持续时间
      this.danmuList.push({ text, top, duration });

      // 弹幕播放完成后移除
      setTimeout(() => {
        this.danmuList.shift();
      }, duration * 1000);
    }
  }
};
</script>

<style>
.danmu-container {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
  background: #000;
}

.danmu-item {
  position: absolute;
  white-space: nowrap;
  color: #fff;
  animation: danmuMove linear;
}

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

使用 Vue 过渡动画

通过 Vue 的 <transition-group> 实现弹幕的进入和离开动画。

<template>
  <div class="danmu-container">
    <transition-group name="danmu">
      <div 
        v-for="(item, index) in danmuList" 
        :key="index" 
        class="danmu-item"
        :style="{ top: `${item.top}px` }"
      >
        {{ item.text }}
      </div>
    </transition-group>
  </div>
</template>

<script>
export default {
  data() {
    return {
      danmuList: [],
      containerHeight: 300
    };
  },
  methods: {
    addDanmu(text) {
      const top = Math.random() * this.containerHeight;
      this.danmuList.push({ text, top });

      setTimeout(() => {
        this.danmuList.shift();
      }, 5000);
    }
  }
};
</script>

<style>
.danmu-container {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
  background: #000;
}

.danmu-item {
  position: absolute;
  white-space: nowrap;
  color: #fff;
  animation: danmuMove 5s linear;
}

@keyframes danmuMove {
  from { transform: translateX(100%); }
  to { transform: translateX(-100%); }
}

.danmu-enter-active, .danmu-leave-active {
  transition: all 0.5s;
}
.danmu-enter, .danmu-leave-to {
  opacity: 0;
}
</style>

使用第三方库

如果需要更复杂的功能(如弹幕碰撞检测、颜色自定义等),可以使用第三方库如 rc-bullets

vue实现弹幕

安装:

npm install rc-bullets

使用:

<template>
  <div ref="danmuContainer" style="width: 100%; height: 300px;"></div>
</template>

<script>
import { Bullet } from 'rc-bullets';

export default {
  mounted() {
    const screen = new Bullet(this.$refs.danmuContainer, {
      trackHeight: 30,
      onStart: () => {
        screen.push('这是一条弹幕');
      }
    });
  }
};
</script>

弹幕功能优化

  1. 性能优化:避免频繁操作 DOM,可以使用虚拟列表技术。
  2. 弹幕防撞:计算弹幕的宽度和速度,避免重叠。
  3. 自定义样式:支持颜色、字体大小等自定义。
  4. 暂停和继续:通过控制 CSS 动画的 animation-play-state 实现。

以上方法可以根据实际需求选择或组合使用。

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

相关文章

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted 和…

vue实现贴图

vue实现贴图

Vue 实现贴图功能 在Vue中实现贴图功能通常涉及将图片动态添加到指定位置或元素上。以下是几种常见实现方式: 使用v-bind动态绑定图片 通过数据驱动的方式动态更换图片源: <templ…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

vue 实现树状

vue 实现树状

Vue 实现树状结构的方法 递归组件实现树状结构 递归组件是 Vue 中实现树状结构的常见方法。通过组件自身调用自身,可以轻松构建多层级树状结构。 <template> <ul…

vue实现座位

vue实现座位

Vue实现座位布局 使用Vue实现座位布局可以通过组件化和响应式数据管理来简化开发流程。以下是一个完整的实现方案: 数据准备 定义座位数据结构,通常使用二维数组表示行列关系: data() {…