当前位置:首页 > 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>

使用弹幕组件

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

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

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

弹幕动画优化

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

.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),
};

弹幕自动清理

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

vue实现发送弹幕

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

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

相关文章

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue实现fullpage

vue实现fullpage

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

vue computed实现

vue computed实现

计算属性的基本用法 在Vue中,计算属性通过computed选项定义,用于声明依赖于其他属性的动态值。计算属性基于它们的依赖进行缓存,只有依赖发生变化时才会重新计算。 new Vue({ dat…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…

vue弹幕实现

vue弹幕实现

Vue弹幕实现方法 弹幕功能常见于视频直播或评论区,以下是基于Vue的实现方案,涵盖基础弹幕、动画控制及性能优化。 基础弹幕渲染 通过v-for动态渲染弹幕列表,结合CSS实现横向移动效果。弹幕数据…

vue逻辑实现怎么实现

vue逻辑实现怎么实现

Vue 逻辑实现方法 Vue 的逻辑实现主要通过组件化、响应式数据、计算属性、方法、生命周期钩子等方式完成。以下是一些核心实现方法: 数据驱动与响应式 在 Vue 中,通过 data 选项定义响应式…