当前位置:首页 > VUE

vue实现发送弹幕

2026-01-17 18:23:21VUE

Vue 实现发送弹幕功能

创建弹幕组件

在 Vue 项目中创建一个弹幕组件 Danmu.vue,用于显示和管理弹幕。组件包含弹幕容器和发送弹幕的表单。

<template>
  <div class="danmu-container">
    <div class="danmu-display" ref="danmuDisplay">
      <div
        v-for="(danmu, index) in danmuList"
        :key="index"
        class="danmu-item"
        :style="{
          top: `${danmu.top}px`,
          color: danmu.color,
          fontSize: `${danmu.size}px`,
        }"
      >
        {{ danmu.text }}
      </div>
    </div>
    <div class="danmu-input">
      <input v-model="inputText" placeholder="输入弹幕内容" />
      <button @click="sendDanmu">发送</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputText: "",
      danmuList: [],
      colors: ["#ff0000", "#00ff00", "#0000ff", "#ffff00", "#ff00ff"],
    };
  },
  methods: {
    sendDanmu() {
      if (!this.inputText.trim()) return;
      const danmu = {
        text: this.inputText,
        top: Math.random() * 200,
        color: this.colors[Math.floor(Math.random() * this.colors.length)],
        size: Math.floor(Math.random() * 10 + 14),
      };
      this.danmuList.push(danmu);
      this.inputText = "";
      setTimeout(() => {
        this.danmuList.shift();
      }, 5000);
    },
  },
};
</script>

<style>
.danmu-container {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
  border: 1px solid #ccc;
}
.danmu-display {
  position: relative;
  width: 100%;
  height: 100%;
}
.danmu-item {
  position: absolute;
  white-space: nowrap;
  animation: move 10s linear;
}
@keyframes move {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(-100%);
  }
}
.danmu-input {
  margin-top: 10px;
}
</style>

使用弹幕组件

在父组件中引入并使用弹幕组件,确保弹幕功能可以正常工作。

vue实现发送弹幕

<template>
  <div>
    <Danmu />
  </div>
</template>

<script>
import Danmu from "./Danmu.vue";
export default {
  components: {
    Danmu,
  },
};
</script>

弹幕动画优化

通过 CSS 动画控制弹幕的移动速度和方向,确保弹幕从右向左平滑移动。

vue实现发送弹幕

.danmu-item {
  animation: move 10s linear;
}
@keyframes move {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(-100%);
  }
}

弹幕随机样式

为弹幕添加随机颜色、大小和垂直位置,增强视觉效果。

const danmu = {
  text: this.inputText,
  top: Math.random() * 200,
  color: this.colors[Math.floor(Math.random() * this.colors.length)],
  size: Math.floor(Math.random() * 10 + 14),
};

弹幕自动清理

设置定时器自动清理过期的弹幕,防止弹幕列表无限增长。

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

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

相关文章

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&…