美文网首页
vue2做的弹幕效果

vue2做的弹幕效果

作者: IamaStupid | 来源:发表于2022-06-24 09:33 被阅读0次

vue2做的弹幕效果

image.png
<template>
  <div class="view-content home">
    <button @click="toggleDanmuAnimate">stop</button>
    <div class="danmu-box">
      <template v-for="(item, i) in danmuList">
        <div class="danmu-item" :key="'dm' + i" :style="{'left': item.left + 'px', top: item.top + 'px'}">{{item.text}}</div>
      </template>
    </div>
  </div>
</template>
<script>
let originData = []
let reqFrame = null
export default {
  name: "home-view",
  data: function () {
    return {
      btnToggle: false,
      winSize: null,
      addIndex: -1,
      danmuList: [],
      danmuBoxHeight: 0,
      danmuSpeed: 1,
      turnTime: 0,
      addOnceNum: 3
    };
  },
  created() {
    this.getWinInfo()
    this.danmuBoxHeight = 410 * this.winSize.rem / 100
    this.ajax()
  },
  methods:{
    getWinInfo() {
      let win = {
        w: window.innerWidth, h: window.innerHeight, rem: 0
      }
      let doc = document.documentElement.style.fontSize.replace('px', '') - 0
      if (doc > 0) {
        win.rem = doc
      }
      this.winSize = win
    },
    ajax() {
      setTimeout(() => {
        let data = [
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
          {text: '1111111111111111'},
          {text: 'tttttttt'},
        ]
        originData = data
        this.animateRequest()
      }, 2000)
    },
    isOutDanmu(item) {
      return item.left <= (this.winSize.w * -1)
    },
    animateRequest() {
      let tempArr = []
      let addNum = Math.floor(Math.random() * this.addOnceNum)
      if (this.addIndex === -1) {
        // 第一次 如果随机添加的数量是0,强制添加1个
        this.turnTime = 110
        if (addNum < 1) {
          addNum = 1
        }
      }
      // console.log('addNum:', addNum)
      if (addNum > 0 && this.turnTime > 100) {
        tempArr = originData.slice(this.addIndex + 1, this.addIndex + 1 + addNum)
        this.addIndex = this.addIndex + addNum
        this.turnTime = 0
      }
      tempArr = [].concat(this.danmuList, tempArr)
      tempArr.forEach((item, i) => {
        if(item.top === undefined || this.isOutDanmu(item)) {
          if (i === 0) {
            item.top = Math.floor(this.danmuBoxHeight / 2 - 50)
          } else {
            item.top = Math.floor(Math.random() * (this.danmuBoxHeight - 30))
            // 为防止弹幕重合
            if (Math.abs(item.top - tempArr[i - 1].top) < 25) {
              // 有可能重合了
              if (item.top > tempArr[i - 1].top) {
                // 两个60 25没有特殊联系,估摸着写的, 主要是为了不让弹幕的位置太靠下
                if (item.top > (this.danmuBoxHeight - 60)) {
                  item.top = item.top - 60
                } else {
                  item.top = item.top + 25
                }
              } else {
                item.top = item.top - 25
              }
            }
          }
          if (item.top < 0) {
            item.top = 0
          }
        }
        if(item.left === undefined || this.isOutDanmu(item)) {
          item.left = Math.floor(this.winSize.w * 1.1)
        } else {
          item.left -= this.danmuSpeed
          if (this.isOutDanmu(item)) {
            tempArr.splice(i, 1)
          }
        }
      })
      this.danmuList = tempArr
      this.turnTime++
      if (tempArr.length === 0) {
        this.addIndex = -1
      }
      reqFrame = window.requestAnimationFrame(this.animateRequest)
      // test code
      // if (this.addIndex < 50) {
      //   window.requestAnimationFrame(this.animateRequest)
      // }
    },
    toggleDanmuAnimate() {
      this.btnToggle = !this.btnToggle
      if (this.btnToggle) {
        window.cancelAnimationFrame(reqFrame);
        reqFrame = null
      } else {
        reqFrame = window.requestAnimationFrame(this.animateRequest)
      }
    }
  }
};
</script>
 <style lang="less" scoped>
 .danmu-box {
   width: 100%; height: 410px; position: absolute; left: 0; top: 266px; overflow: hidden; background: #f00;
   .danmu-item {
     display: inline-block; padding: 3px 8px; border-radius: 6px; background-color: rgba(0, 0, 0, 0.5);
     position: absolute; left: 110%; color: #fff;
   }
 }
</style>

相关文章

  • vue2做的弹幕效果

    vue2做的弹幕效果

  • iOS 弹幕效果

    iOS 弹幕效果 iOS 弹幕效果

  • 弹幕效果

    现在我们会经常看一些直播,会经常用一些直播软件,这些软件有一个很好的功能,就是我们可以在上面发送弹幕交流娱乐,今...

  • Flutter 实现虎牙/斗鱼 弹幕效果

    老孟导读:用Flutter实现弹幕功能,轻松实现虎牙、斗鱼的弹幕效果。 先来一张效果图: 实现原理 弹幕的实现原理...

  • 各种弹幕实现研究

    1、弹幕效果 2、旋转效果 2.旋转木马 3、改变透明度效果 4、超链接弹幕效果 5、绘图+渐隐 效果 代码: 6...

  • 简单的弹幕效果

  • iOS 弹幕效果

    项目中视频直播有个弹幕功能,网上也有很多弹幕的demo,之前忙于项目进度,没有细致研究其中是如何实现的。如今闲暇之...

  • Android弹幕效果

    上面效果图中白色的背景就是弹幕本身,是一个自定义的FrameLayout,我这里是为了更好的展示弹幕的位置才设置成...

  • iOS直播系列之《很会飞的弹幕》

    前言 此弹幕来源于直播,所以名为 LiveBarrage 。 弹幕效果: 弹幕君说: 我会飞~~(gun,你咋不上...

  • iOS 简单实现按照轨道弹幕效果

    ios 铺满轨道弹幕效果实现 为避免重复造轮子,网上查找了两个弹幕效果 OCBarrage[https://git...

网友评论

      本文标题:vue2做的弹幕效果

      本文链接:https://www.haomeiwen.com/subject/zkmlcrtx.html