当前位置:首页 > 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 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm inst…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&g…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成…

vue公共列表的实现

vue公共列表的实现

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

vue异步组建实现原理

vue异步组建实现原理

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

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…