雪花飘落-2022虎年大吉

前言

临近虎年春节,又缝下雪,简单弄个雪花飘落,全当玩耍

效果图

在这里插入图片描述

代码展示

代码很简单,一些css,js,基本的html,加上图片就完事了

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Snow</title>
  <link rel="stylesheet" href="./css/index.css">
</head>
<body>
  <div class="avatar"></div>
  <div class="avatar"></div>
  <div class="avatar"></div>
  <div class="avatar"></div>
</body>
<script src="./js/index.js"></script>
</html>
css
html,body{
  width: 100%;
  height: 100%;
  background-color: black;
  margin: 0;
  padding: 0;
  color: #ffffff;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}
.avatar{
  width: 50%;
  height: 100px;
  border-radius: 20px;
  opacity: 0.8;
  background-image: url('../imgs/tiger.png');
  background-repeat: no-repeat;
  background-size: 100px 100px;
  background-position: center;
}
.snow-item{
  display: inline-block;
  border-radius: 50%;
  position: absolute;
  animation: snowDown linear;
  background-image: url('../imgs/snow.png');
  background-repeat: no-repeat;
  background-size: contain;
  background-position: center;
}

@keyframes snowDown {
  from {
    top: 0;
  }
  to {
    top: 110%;
  }
}
js
class Snow {
  defaultOption = {
    maxSize: 10,// 最大直径
    minSize: 2,// 最小直径
    bodyWidth: document.body.clientWidth,// body宽度
    bodyHeight: document.body.clientHeight,// body高度
    duration: 10,// 飘落周期
  };
  options = null;
  timer = null;
  queenList = [];
  constructor(options){
    // 整合用户配置
    this.options = Object.assign(this.defaultOption, options);
    this.init();
  }

  init(){
    this.create();
    this.setListener();
  }

  create(){
    const { maxSize, minSize, bodyWidth, duration } = this.options;
    const size = Number(((maxSize - minSize) * Math.random()).toFixed(1)) + minSize;
    const opacity = (0.6 + 0.4 * Math.random()).toFixed(2);
    const aSnow = document.createElement('span');
    this.ele = aSnow;
    aSnow.style.width = size + 'px';
    aSnow.style.height = size + 'px';
    aSnow.style.left = (bodyWidth - 10) * Math.random() + 'px';
    aSnow.style.top = '110%';
    aSnow.style.opacity = opacity;
    aSnow.style.animationDuration = duration + 's';
    aSnow.className = 'snow-item';
    document.getElementsByTagName('body')[0].appendChild(aSnow);
  }

  setListener(){
    const { duration } = this.options;
    setTimeout(() => {
      this.destroy()
    }, duration * 1000);
  }

  destroy(){
    this.ele.remove();
  }
}

setInterval(() => {
  new Snow({ minSize: 10, maxSize: 20 });
}, 300);

结尾

怎么样,是不是超简单,哈哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值