当前位置:首页 > VUE

vue实现发送弹幕

2026-02-18 10:50:25VUE

Vue实现发送弹幕功能

核心思路

通过Vue的数据绑定和列表渲染能力,结合CSS动画实现弹幕从右向左移动的效果。弹幕数据存储在数组中,动态添加新弹幕并控制其样式和位置。

实现步骤

1. 创建Vue实例与数据结构

new Vue({
  el: '#app',
  data: {
    danmuList: [], // 存储弹幕对象
    inputText: ''   // 输入框内容
  }
})

2. 弹幕发送方法

vue实现发送弹幕

methods: {
  sendDanmu() {
    if (!this.inputText.trim()) return

    const newDanmu = {
      id: Date.now(),
      text: this.inputText,
      top: Math.random() * 80 + '%', // 随机垂直位置
      color: `rgb(${
        Math.floor(Math.random() * 256)
      }, ${
        Math.floor(Math.random() * 256)
      }, ${
        Math.floor(Math.random() * 256)
      })` // 随机颜色
    }

    this.danmuList.push(newDanmu)
    this.inputText = ''
  }
}

3. 弹幕容器与样式

<div class="danmu-container">
  <div 
    v-for="item in danmuList" 
    :key="item.id"
    class="danmu-item"
    :style="{
      color: item.color,
      top: item.top
    }"
  >
    {{ item.text }}
  </div>
</div>
.danmu-container {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
  background: #000;
}

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

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

4. 输入框与发送按钮

vue实现发送弹幕

<div class="input-area">
  <input v-model="inputText" @keyup.enter="sendDanmu">
  <button @click="sendDanmu">发送</button>
</div>

高级优化方向

性能优化

  • 使用虚拟滚动技术处理大量弹幕
  • 对离开屏幕的弹幕进行回收利用
  • 添加弹幕防撞检测算法

功能扩展

  • 实现弹幕暂停/继续功能
  • 添加弹幕速度控制选项
  • 支持图片/表情弹幕
  • 添加弹幕屏蔽过滤功能

完整示例代码

new Vue({
  el: '#app',
  data: {
    danmuList: [],
    inputText: ''
  },
  methods: {
    sendDanmu() {
      if (!this.inputText.trim()) return

      this.danmuList.push({
        id: Date.now(),
        text: this.inputText,
        top: Math.random() * 80 + '%',
        color: `rgb(${Math.random() * 255},${Math.random() * 255},${Math.random() * 255})`
      })

      this.inputText = ''
    }
  }
})
<div id="app">
  <div class="danmu-container">
    <div 
      v-for="item in danmuList" 
      :key="item.id"
      class="danmu-item"
      :style="{ color: item.color, top: item.top }"
    >
      {{ item.text }}
    </div>
  </div>

  <div class="input-area">
    <input v-model="inputText" @keyup.enter="sendDanmu">
    <button @click="sendDanmu">发送</button>
  </div>
</div>

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

相关文章

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <templat…

vue实现积分

vue实现积分

Vue 实现积分功能 在 Vue 中实现积分功能通常涉及前端展示、交互逻辑以及与后端 API 的通信。以下是常见的实现方法和步骤: 积分展示组件 创建一个积分展示组件,用于显示当前用户的积分。可以使…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…