当前位置:首页 > VUE

vue实现弹幕

2026-03-07 07:45:51VUE

Vue 实现弹幕功能

弹幕功能常见于视频直播或播放场景,通过 Vue 可以实现动态、可配置的弹幕效果。以下是实现弹幕的几种方法:

基于 CSS 动画的弹幕

通过 CSS 的 animationtransition 控制弹幕的移动效果,结合 Vue 的动态渲染能力实现弹幕生成。

<template>
  <div class="danmu-container">
    <div 
      v-for="(item, index) in danmuList" 
      :key="index" 
      class="danmu-item"
      :style="{ top: item.top + 'px', color: item.color }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      danmuList: [],
      colors: ['#ff0000', '#00ff00', '#0000ff', '#ffff00']
    };
  },
  methods: {
    addDanmu(text) {
      const top = Math.floor(Math.random() * 200);
      const color = this.colors[Math.floor(Math.random() * this.colors.length)];
      this.danmuList.push({ text, top, color });

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

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

.danmu-item {
  position: absolute;
  white-space: nowrap;
  animation: move 5s linear;
}

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

基于 Vue Transition 的弹幕

利用 Vue 的 <transition-group> 实现弹幕的进入和离开动画,更灵活地控制弹幕行为。

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

<script>
export default {
  data() {
    return {
      danmuList: [],
      colors: ['#ff0000', '#00ff00', '#0000ff', '#ffff00'],
      idCounter: 0
    };
  },
  methods: {
    addDanmu(text) {
      const top = Math.floor(Math.random() * 200);
      const color = this.colors[Math.floor(Math.random() * this.colors.length)];
      this.danmuList.push({ 
        id: this.idCounter++,
        text, 
        top, 
        color 
      });

      setTimeout(() => {
        this.danmuList = this.danmuList.filter(item => item.id !== this.idCounter - 1);
      }, 5000);
    }
  }
};
</script>

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

.danmu-item {
  position: absolute;
  white-space: nowrap;
  transition: transform 5s linear;
  transform: translateX(100%);
}

.danmu-enter-active {
  transition: transform 5s linear;
}
.danmu-enter {
  transform: translateX(100%);
}
.danmu-enter-to {
  transform: translateX(-100%);
}
</style>

基于 Canvas 的高性能弹幕

对于大量弹幕的场景,使用 Canvas 渲染性能更高,适合高并发的弹幕需求。

vue实现弹幕

<template>
  <canvas ref="canvas" width="800" height="200"></canvas>
</template>

<script>
export default {
  data() {
    return {
      danmuList: [],
      colors: ['#ff0000', '#00ff00', '#0000ff', '#ffff00'],
      ctx: null,
      animationId: null
    };
  },
  mounted() {
    this.ctx = this.$refs.canvas.getContext('2d');
    this.renderDanmu();
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId);
  },
  methods: {
    addDanmu(text) {
      const y = Math.floor(Math.random() * 180);
      const color = this.colors[Math.floor(Math.random() * this.colors.length)];
      this.danmuList.push({
        text,
        x: 800,
        y,
        color,
        speed: 2 + Math.random() * 3
      });
    },
    renderDanmu() {
      this.ctx.clearRect(0, 0, 800, 200);

      this.danmuList.forEach((item, index) => {
        item.x -= item.speed;
        this.ctx.fillStyle = item.color;
        this.ctx.fillText(item.text, item.x, item.y);
      });

      this.danmuList = this.danmuList.filter(item => item.x > -100);
      this.animationId = requestAnimationFrame(this.renderDanmu);
    }
  }
};
</script>

弹幕功能扩展

  • 弹幕速度控制:通过调整 speed 属性实现不同弹幕的移动速度差异。
  • 弹幕防遮挡:动态计算弹幕位置,避免重叠。
  • 弹幕互动:监听点击事件,支持点击弹幕触发特定行为。

以上方法可根据实际需求选择,CSS 动画适合简单场景,Canvas 适合高性能需求。

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

相关文章

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现socket

vue实现socket

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

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue实现ios

vue实现ios

Vue 实现 iOS 风格应用 使用 Vue 实现 iOS 风格的应用,可以通过结合 UI 框架和自定义样式来达成目标。以下是具体方法和步骤: 选择 iOS 风格的 UI 框架 Vue 生态中有多…

vue实现portal

vue实现portal

Vue 实现 Portal 功能 Portal 是一种将子节点渲染到父组件 DOM 层级之外的 DOM 节点的技术,常用于实现模态框、弹出层等需要脱离当前组件层级的场景。Vue 可以通过多种方式实现…