当前位置:首页 > VUE

vue实现弹幕

2026-01-13 00:36:14VUE

Vue 实现弹幕功能

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

使用 CSS 动画实现基础弹幕

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

<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

安装:

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 中实现视频开场动画可以通过结合 HTML5 的 <video> 标签和 Vue 的过渡效果或生命周期钩子来完成。以下是具体实现方法: 安装依…

vue实现按卡片轮播

vue实现按卡片轮播

实现卡片轮播的基本思路 在Vue中实现卡片轮播可以通过结合v-for指令和动态样式绑定完成。核心是维护一个当前显示卡片的索引,通过CSS过渡效果实现平滑切换。 基础实现步骤 模板部分 使用v-for…

vue设计与实现 书

vue设计与实现 书

vue设计与实现 书 《Vue.js设计与实现》是一本深入探讨Vue.js框架内部原理和设计思想的书籍,适合希望深入理解Vue.js的开发者。以下是关于这本书的详细信息: 书籍内容 《Vue…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在V…