当前位置:首页 > 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中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现ssr

vue实现ssr

Vue 实现 SSR(服务端渲染) Vue 的服务端渲染(SSR)允许在服务器端生成完整的 HTML 内容,再发送到客户端,提升首屏加载速度和 SEO 友好性。以下是实现 Vue SSR 的关键步骤和…

vue模版实现

vue模版实现

Vue 模板实现方法 Vue 模板是 Vue.js 的核心特性之一,用于声明式地将 DOM 绑定至底层 Vue 实例的数据。以下是几种常见的 Vue 模板实现方式: 单文件组件(SFC) 使用 .v…

vue原生实现

vue原生实现

Vue 原生实现方法 在 Vue 中实现原生功能通常指直接使用 Vue 的核心特性(如指令、响应式数据、组件等)而非依赖第三方库。以下是几种常见场景的原生实现方式: 数据绑定与响应式更新 Vue 的…

vue实现cs程序

vue实现cs程序

Vue 实现 CS(客户端-服务器)程序 Vue 本身是一个前端框架,主要用于构建用户界面。要实现一个完整的 CS(客户端-服务器)程序,需要结合后端技术。以下是实现的基本思路和步骤: 前端部分(V…