漂浮物vue3,只占2%gpu

29 篇文章 1 订阅
<template>
  <div id="floatImg"></div>
</template>
<script>
import { defineComponent, onMounted, ref } from 'vue';
export default defineComponent({
  name: 'float',
  props: {
    floatImage: {
      type: String,
      default: ''
    }
  },
  setup(props) {
    const innerWidth = ref(375);
    onMounted(() => {
      document.addEventListener('visibilitychange', function () {
        switch (document.visibilityState) {
          case 'visible': // alert("处于正常打开");
            $('#floatImg').empty();
            break;
        }
      });
      setInterval(() => {
        let left;
        if (innerWidth.value > 375) {
          left = Math.random() * innerWidth.value;
        } else {
          left = Math.random() * 375;
        }
        let src = props.floatImage;
        isSnow(left, src);
      }, 1000);
    });
    const isSnow = (left, src) => {
      let floatImg = document.getElementById('floatImg');
      let img = document.createElement('img');
      img.className = 'roll roll' + Math.floor(Math.random() * 3 + 3);
      img.src = src;
      img.style.width = Math.random() * (1.1 - 0.7) + 0.7 + 'rem';
      img.style.height = 'auto';
      img.style.left = left + 'px';
      img.style.bottom = '100%';
      floatImg.appendChild(img);
      setTimeout(() => {
        floatImg.removeChild(img);
      }, 20000);
    };
    return {
      isSnow,
      innerWidth
    };
  }
});
</script>

<style>
#floatImg {
  width: 375px;
  max-width: 375px;
  overflow: hidden;
}
@keyframes mysnow {
  0% {
    bottom: 100%;
    transform: rotate(0deg);
  }
  100% {
    bottom: -10%;
    transform: rotate(-30deg);
  }
}
@-webkit-keyframes mysnow {
  0% {
    bottom: 100%;
    -webkit-transform: rotate(0deg);
  }
  100% {
    bottom: -10%;
    -webkit-transform: rotate(-30deg);
  }
}
@-moz-keyframes mysnow {
  0% {
    bottom: 100%;
    -moz-transform: rotate(0deg);
  }
  100% {
    bottom: -10%;
    -moz-transform: rotate(-30deg);
  }
}
@-ms-keyframes mysnow {
  0% {
    bottom: 100%;
    -ms-transform: rotate(0deg);
  }
  100% {
    bottom: -10%;
    -ms-transform: rotate(-30deg);
  }
}
@-o-keyframes mysnow {
  0% {
    bottom: 100%;
    -o-transform: rotate(00deg);
  }
  100% {
    bottom: -10%;
    -o-transform: rotate(-30deg);
  }
}
.roll5 {
  animation: mysnow 20s linear;
  -webkit-animation: mysnow 20s linear;
  -moz-animation: mysnow 20s linear;
  -ms-animation: mysnow 20s linear;
  -o-animation: mysnow 20s linear;
}
.roll4 {
  animation: mysnow 16s linear;
  -webkit-animation: mysnow 16s linear;
  -moz-animation: mysnow 16s linear;
  -ms-animation: mysnow 16s linear;
  -o-animation: mysnow 16s linear;
}
.roll3 {
  animation: mysnow 12s ease-out;
  -webkit-animation: mysnow 12s ease-out;
  -moz-animation: mysnow 12s ease-out;
  -ms-animation: mysnow 12s ease-out;
  -o-animation: mysnow 12s ease-out;
}
.roll {
  position: fixed;
  margin-left: calc(50vw - (375px / 2));
  z-index: 1;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值