当前位置:首页 > 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 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue实现京东

vue实现京东

Vue 实现京东电商网站的关键步骤 项目搭建与基础配置 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理路由,Vuex/Pinia 管理状态。配置基础 UI 框架如 E…

vue实现队列

vue实现队列

Vue 实现队列功能 在 Vue 中实现队列功能可以通过多种方式完成,以下是几种常见的方法: 使用数组模拟队列 队列遵循先进先出(FIFO)原则,可以用数组的 push 和 shift 方法模拟入队…

react实现vue

react实现vue

React 实现 Vue 功能 React 和 Vue 是两种不同的前端框架,但可以通过一些方法在 React 中实现 Vue 的特性。以下是几种常见 Vue 功能在 React 中的实现方式: 双…

vue日历实现

vue日历实现

实现基础日历布局 使用Vue的模板语法构建日历的基本HTML结构。一个典型的日历包含星期标题和日期格子,可以通过v-for循环生成。月份切换功能通过计算属性动态更新日期数组。 <templat…