当前位置:首页 > 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),
};

弹幕自动清理

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

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

vue实现发送弹幕

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

相关文章

vue实现组件循环

vue实现组件循环

Vue 组件循环的实现方法 在 Vue 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。…

vue实现皮肤切换

vue实现皮肤切换

实现皮肤切换的基本思路 在Vue中实现皮肤切换功能,通常涉及动态修改CSS变量或类名。核心是通过状态管理当前主题,并在组件中应用对应的样式。 使用CSS变量实现主题切换 CSS变量(自定义属性)是实…

vue异步组建实现原理

vue异步组建实现原理

Vue 异步组件实现原理 Vue 的异步组件允许将组件按需加载,通常用于代码分割或延迟加载非关键组件。其核心原理基于 Promise 和动态导入。 基本实现方式 通过 defineAsyncComp…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件,…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogres…

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式:…