当前位置:首页 > 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 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue单页面实现登录

vue单页面实现登录

实现登录功能的基本流程 在Vue单页面应用中实现登录功能,通常需要以下几个关键步骤: 创建登录表单组件 使用Vue的模板语法创建包含用户名和密码输入框的表单,并绑定数据到组件的data属性。 &l…

vue实现文字播放栏

vue实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <tem…

vue实现页面截屏

vue实现页面截屏

使用html2canvas库实现截屏 html2canvas是一个流行的JavaScript库,可以将DOM元素转换为Canvas图像。在Vue项目中安装html2canvas: npm insta…

vue实现本地数据存储

vue实现本地数据存储

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

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-li…