哄另一半开心的下雨特效

14 篇文章 0 订阅

在不下雨的天气,发给另一半这个下雨特效的html文件,她能开心一整天!!!

看效果

附上源码

HTML+CSS代码

<!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>Document</title>
    <style>
        body {
            background-color: black;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <script src="rain.js"></script>
    <script>
        var arr = [];
        animation();
        function animation() {
            requestAnimationFrame(animation);
            for (var i = 0; i < random(3, 6); i++) {
                const rain = new ClsRain();
                rain.appendTo('body');
                arr.push(rain);
            }
            arr.forEach((item, key) => {
                item.update();
                if (!item)
                    arr.splice(key, 1);
            })
        }
        function random(min, max) {
            return parseInt(Math.random() * (max + 1 - min) + min);
        }
    </script>
</body>

</html>

JS代码 

class ClsRain{
    constructor(){
        this.elem;
        this.rain;
        this.x;
        this.y;
        this.speed=this.random(10,30);
    }
    random(min,max){
        return parseInt(Math.random()*(max+1-min)+min);
    }
    fnCreateTheRain(element){
        this.rain=document.createElement('div');
        this.rain.style.width=3+'px';
        this.rain.style.height=10+'px';
        this.rain.style.borderRadius=2+'px';
        this.rain.style.backgroundColor="white";
        this.rain.style.position="absolute";
        this.rain.style.top=-10+'px';
        this.y=-10;
        this.x=this.random(0,this.elem.clientWidth);
        console.log(this.elem.off);
        this.rain.style.left=this.x+'px';
        element.appendChild(this.rain);
        
    }
    appendTo(element){
        if(typeof element==='string'){
            this.elem=document.querySelector(element);
        }
        this.fnCreateTheRain(this.elem);
    }
    update(){
        if(!this.rain)
            return;
        console.log(this.y);
        this.y+=this.speed;
        this.rain.style.top=this.y+'px';
        if(this.y>document.documentElement.clientHeight){
            this.rain.remove();
            this.rain=null;
        }
    }
}

 不会吧,不会吧,不会你还没有另一半吧。嘻嘻。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值