当前位置:首页 > 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组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return { f…

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓、面…

vue实现骰子

vue实现骰子

Vue 实现骰子功能 创建 Vue 组件 在 Vue 项目中创建一个骰子组件,例如 Dice.vue。该组件包含骰子的外观和逻辑。 <template> <div class…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind…

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…