当前位置:首页 > 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>

样式部分

vue 实现弹幕js

.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定时器的实现

模板部分

vue 实现弹幕js

<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;
  });
}

实现弹幕暂停/继续

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

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

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

相关文章

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件:…

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(…

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖…

computed vue 实现

computed vue 实现

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

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…