当前位置:首页 > 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绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue实现eventbus

vue实现eventbus

Vue 中实现 EventBus 在 Vue 中,EventBus 是一种跨组件通信的机制,尤其适用于非父子组件之间的数据传递。以下是实现 EventBus 的几种方法: 方法一:使用 Vue 实例…

vue指令实现

vue指令实现

Vue 指令的实现方式 Vue 指令是特殊的标记,用于在 DOM 元素上应用特定的行为。可以通过全局或局部方式注册自定义指令。 全局注册指令 在 Vue 应用中通过 Vue.directive 方法…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…