当前位置:首页 > VUE

vue实现弹幕

2026-02-10 07:55:07VUE

Vue 实现弹幕功能

弹幕功能的实现可以分为几个关键部分:弹幕数据的生成、弹幕的渲染、弹幕的动画控制以及用户交互。

弹幕数据管理

弹幕数据通常是一个数组,每个元素包含弹幕内容、颜色、速度、位置等信息。可以使用 Vue 的 dataref 来管理弹幕数据。

data() {
  return {
    danmuList: [],
    colors: ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff'],
    speeds: [5, 6, 7, 8, 9],
  }
}

弹幕生成

弹幕可以通过用户输入或自动生成。可以使用 setInterval 定时生成弹幕,或通过事件触发。

vue实现弹幕

methods: {
  addDanmu(text) {
    const danmu = {
      id: Date.now(),
      text,
      color: this.colors[Math.floor(Math.random() * this.colors.length)],
      speed: this.speeds[Math.floor(Math.random() * this.speeds.length)],
      top: Math.floor(Math.random() * 100),
    }
    this.danmuList.push(danmu)
  }
}

弹幕渲染

使用 Vue 的 v-for 指令渲染弹幕列表,并通过 CSS 控制弹幕的样式和位置。

<template>
  <div class="danmu-container">
    <div
      v-for="danmu in danmuList"
      :key="danmu.id"
      class="danmu-item"
      :style="{
        color: danmu.color,
        top: `${danmu.top}%`,
        animationDuration: `${10 - danmu.speed}s`,
      }"
    >
      {{ danmu.text }}
    </div>
  </div>
</template>

弹幕动画

通过 CSS 动画实现弹幕的移动效果。弹幕从右向左移动,可以通过 transformleft 属性控制。

vue实现弹幕

.danmu-container {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
}

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

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

用户交互

可以通过输入框让用户发送弹幕,并监听回车事件触发弹幕生成。

<input
  v-model="inputText"
  @keyup.enter="addDanmu(inputText)"
  placeholder="输入弹幕内容"
/>

弹幕清理

弹幕移出屏幕后需要清理,避免内存泄漏。可以通过监听动画结束事件移除弹幕。

methods: {
  handleAnimationEnd(id) {
    this.danmuList = this.danmuList.filter(danmu => danmu.id !== id)
  }
}
<div
  v-for="danmu in danmuList"
  :key="danmu.id"
  class="danmu-item"
  @animationend="handleAnimationEnd(danmu.id)"
>
  {{ danmu.text }}
</div>

完整示例代码

<template>
  <div>
    <input
      v-model="inputText"
      @keyup.enter="addDanmu(inputText)"
      placeholder="输入弹幕内容"
    />
    <div class="danmu-container">
      <div
        v-for="danmu in danmuList"
        :key="danmu.id"
        class="danmu-item"
        :style="{
          color: danmu.color,
          top: `${danmu.top}%`,
          animationDuration: `${10 - danmu.speed}s`,
        }"
        @animationend="handleAnimationEnd(danmu.id)"
      >
        {{ danmu.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputText: '',
      danmuList: [],
      colors: ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff'],
      speeds: [5, 6, 7, 8, 9],
    }
  },
  methods: {
    addDanmu(text) {
      const danmu = {
        id: Date.now(),
        text,
        color: this.colors[Math.floor(Math.random() * this.colors.length)],
        speed: this.speeds[Math.floor(Math.random() * this.speeds.length)],
        top: Math.floor(Math.random() * 100),
      }
      this.danmuList.push(danmu)
    },
    handleAnimationEnd(id) {
      this.danmuList = this.danmuList.filter(danmu => danmu.id !== id)
    },
  },
}
</script>

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

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

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

优化建议

  • 性能优化:对于大量弹幕,可以使用虚拟滚动技术减少 DOM 数量。
  • 碰撞检测:避免弹幕重叠,可以动态调整弹幕的垂直位置。
  • 暂停与恢复:通过控制 CSS 动画的 animation-play-state 实现弹幕的暂停和恢复功能。

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

相关文章

vue实现store

vue实现store

Vue 实现 Store 在 Vue 中,可以通过 Vuex 或 Pinia 实现全局状态管理(Store)。以下是两种主流方案的实现方法。 使用 Vuex 实现 Store Vuex 是 Vue…

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue-i…

vue实现公告

vue实现公告

Vue 实现公告功能的方法 公告功能通常需要实现滚动展示、自动切换或固定显示的效果。以下是几种常见的实现方式: 使用 marquee 标签实现滚动公告 <template> &l…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

vue实现上传

vue实现上传

Vue 文件上传实现方法 使用 Vue 实现文件上传可以通过原生 HTML5 的 <input type="file"> 元素结合 Vue 的事件处理和数据绑定来实现。 基础实现步骤…

vue分页实现

vue分页实现

实现Vue分页的几种方法 使用Element UI的分页组件 安装Element UI库后,可以直接使用其分页组件。以下是一个基本示例: <template> <div>…