当前位置:首页 > VUE

vue弹幕实现不重叠

2026-01-07 02:45:45VUE

实现 Vue 弹幕不重叠的方法

CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。

// 弹幕轨道高度计算
const trackHeight = 30;
const tracks = Math.floor(containerHeight / trackHeight);

随机轨道分配 弹幕发射时随机分配空闲轨道,若当前轨道被占用则选择下一可用轨道。通过数组记录轨道占用状态。

vue弹幕实现不重叠

function getAvailableTrack() {
  for (let i = 0; i < tracks; i++) {
    if (!occupiedTracks[i]) {
      occupiedTracks[i] = true;
      return i;
    }
  }
  return -1; // 无可用轨道时处理
}

碰撞检测算法 使用矩形碰撞检测判断弹幕位置,动态调整新弹幕的发射时间或轨道。通过弹幕宽度和速度计算碰撞可能性。

function checkCollision(barrage, track) {
  const existing = activeBarrages.filter(b => b.track === track);
  return existing.some(b => {
    const distance = b.speed * (Date.now() - b.startTime);
    return distance < containerWidth + barrage.width;
  });
}

动态速度调整 根据弹幕密度自动调整速度,高密度时加快速度减少重叠概率。可设置不同速度档位对应不同轨道数量。

vue弹幕实现不重叠

const speedLevels = [3, 4, 5]; // px/ms
const currentSpeed = speedLevels[Math.floor(activeBarrages.length / 10)];

可视区域外缓冲 在容器外设置缓冲区存放待显示弹幕,通过IntersectionObserver API监测可视区域,动态加载弹幕。

<div class="barrage-buffer" ref="buffer"></div>

性能优化建议 使用虚拟滚动技术,对不可见弹幕进行DOM回收。限制同时显示的弹幕数量,超过阈值时进入队列等待。

const MAX_BARRAGES = 50;
if (activeBarrages.length >= MAX_BARRAGES) {
  queue.push(newBarrage);
}

完整示例组件结构

<template>
  <div class="barrage-container" ref="container">
    <div 
      v-for="(item, index) in activeBarrages" 
      :key="index"
      class="barrage-item"
      :style="{
        top: `${item.track * trackHeight}px`,
        animation: `move ${item.duration}s linear`
      }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      trackHeight: 30,
      occupiedTracks: [],
      activeBarrages: [],
      queue: []
    }
  },
  mounted() {
    this.initTracks();
    this.startEmitter();
  },
  methods: {
    initTracks() {
      const containerHeight = this.$refs.container.clientHeight;
      this.tracks = Math.floor(containerHeight / this.trackHeight);
      this.occupiedTracks = new Array(this.tracks).fill(false);
    },
    emitBarrage(text) {
      const track = this.getAvailableTrack();
      if (track >= 0) {
        this.activeBarrages.push({
          text,
          track,
          duration: this.calcDuration(),
          startTime: Date.now()
        });
        setTimeout(() => {
          this.occupiedTracks[track] = false;
        }, this.duration * 1000);
      } else {
        this.queue.push(text);
      }
    }
  }
}
</script>

<style>
.barrage-container {
  position: relative;
  height: 300px;
  overflow: hidden;
}
.barrage-item {
  position: absolute;
  white-space: nowrap;
  animation-fill-mode: forwards;
}
@keyframes move {
  from { transform: translateX(100%); }
  to { transform: translateX(-100%); }
}
</style>

关键参数调优建议

  • 轨道高度建议30-40px,平衡显示密度与可读性
  • 动画时长控制在5-15秒区间,根据内容长度动态计算
  • 碰撞检测间隔设置为200-300ms,平衡性能与精确度
  • 缓冲区大小建议为可视区域的2-3倍宽度

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

相关文章

vue实现导航栏切换

vue实现导航栏切换

Vue实现导航栏切换的方法 使用v-for和v-bind动态渲染导航项 通过v-for循环遍历导航数据数组,结合v-bind动态绑定class或style。当点击导航项时,更新当前选中项的索引或ID。…

vue实现静态查询

vue实现静态查询

Vue 实现静态查询 在 Vue 中实现静态查询通常指不依赖后端接口,直接在前端处理数据筛选或搜索功能。以下是几种常见实现方式: 使用计算属性过滤数据 通过计算属性对静态数据进行筛选,适合简单查询…

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm ins…

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload(…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…