当前位置:首页 > VUE

vue 实现弹幕js

2026-02-18 23:04:41VUE

Vue 实现弹幕功能

在Vue中实现弹幕功能,可以通过动态渲染DOM元素并结合CSS动画或JavaScript定时器控制移动效果。以下是两种常见实现方式:

基于CSS动画的实现

模板部分

<template>
  <div class="danmu-container">
    <div 
      v-for="(item, index) in danmuList" 
      :key="index" 
      class="danmu-item"
      :style="{
        top: `${item.top}px`,
        animationDuration: `${item.speed}s`
      }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

样式部分

.danmu-container {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
  background: #000;
}

.danmu-item {
  position: absolute;
  white-space: nowrap;
  color: #fff;
  animation: danmuMove linear;
}

@keyframes danmuMove {
  from { transform: translateX(100%); }
  to { transform: translateX(-100%); }
}

逻辑部分

export default {
  data() {
    return {
      danmuList: [],
      colors: ['#ff0000', '#00ff00', '#0000ff', '#ffff00']
    }
  },
  methods: {
    addDanmu(text) {
      const speed = 5 + Math.random() * 5;
      const top = Math.random() * 250;
      const color = this.colors[Math.floor(Math.random() * this.colors.length)];

      this.danmuList.push({
        text,
        speed,
        top,
        color
      });

      setTimeout(() => {
        this.danmuList.shift();
      }, speed * 1000);
    }
  }
}

基于JavaScript定时器的实现

模板部分

<template>
  <div class="danmu-container" ref="container">
    <div 
      v-for="(item, index) in danmuList" 
      :key="index" 
      class="danmu-item"
      :style="{
        top: `${item.top}px`,
        left: `${item.left}px`,
        color: item.color
      }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

逻辑部分

export default {
  data() {
    return {
      danmuList: [],
      timer: null
    }
  },
  mounted() {
    this.timer = setInterval(this.updateDanmuPosition, 16);
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {
    addDanmu(text) {
      const containerWidth = this.$refs.container.offsetWidth;
      const item = {
        text,
        left: containerWidth,
        top: Math.random() * 250,
        color: `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)})`,
        speed: 2 + Math.random() * 3
      };
      this.danmuList.push(item);
    },
    updateDanmuPosition() {
      this.danmuList.forEach(item => {
        item.left -= item.speed;
      });
      this.danmuList = this.danmuList.filter(item => item.left > -100);
    }
  }
}

性能优化建议

  1. 使用虚拟滚动技术处理大量弹幕时,只渲染可视区域内的弹幕元素
  2. 对于频繁变动的数据,使用Object.freeze()冻结不需要响应式的数据
  3. 采用Web Workers处理弹幕位置计算等密集型任务
  4. 使用requestAnimationFrame代替setInterval实现更流畅的动画

扩展功能实现

实现弹幕碰撞检测

checkCollision(newItem) {
  return this.danmuList.some(item => {
    return Math.abs(item.top - newItem.top) < 20 && 
           Math.abs(item.left - newItem.left) < 100;
  });
}

实现弹幕暂停/继续

vue 实现弹幕js

pauseDanmu() {
  cancelAnimationFrame(this.animationId);
},
resumeDanmu() {
  this.updateDanmuPosition();
}

以上实现方式可根据实际需求选择或组合使用。CSS动画实现更简洁,JavaScript控制更灵活。建议在移动端优先考虑CSS动画方案以获得更好的性能表现。

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

相关文章

vue 实现多选

vue 实现多选

Vue 实现多选的方法 在 Vue 中实现多选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定数组 Vue 的 v-model 指令可以与复选框结合使用,绑定到一个数…

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vue…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue实现vmodel

vue实现vmodel

Vue 实现 v-model Vue 的 v-model 是一个语法糖,用于在表单元素或组件上实现双向数据绑定。以下是其实现方式和原理的详细说明。 在表单元素上使用 v-model v-model…

vue实现转盘抽奖

vue实现转盘抽奖

Vue 实现转盘抽奖 基本思路 转盘抽奖的核心是通过 CSS 动画实现旋转效果,Vue 负责控制旋转逻辑和数据绑定。关键点包括转盘扇形区域的绘制、旋转动画的触发与停止、中奖结果的判定。 实现步骤 H…

vue 实现点击选中

vue 实现点击选中

实现点击选中效果 在Vue中实现点击选中效果可以通过多种方式完成,以下是几种常见的方法: 方法一:使用v-bind和v-on 通过绑定class和监听click事件来实现选中状态切换。 <…