JS动画掉落雪花自旋转

原生JS动画掉落雪花自旋转
JS的部分

var Snow=(function () {
    function Snow(parent,left,speed) {
        if(speed){
            this.speed=speed;
        }
        if(left){
            this.left=left;
        }else{
            this.left=Math.floor(Math.random()*parent.clientWidth)
        }
        this.createSnow(parent);
    }
    Snow.prototype={
        snowImg:null,
        speed:2,
        angle:0,
        alpha:1,
        left:0,
        angleSpeed:2,
        scale:1,
        alphaSpeed:0.003,
        createSnow:function (parent) {
            if(!this.snowImg){
                this.snowImg=new Image();
                this.snowImg.src="./src/img/snow.png";
                this.snowImg.style.position="absolute";
                Object.assign(this.snowImg.style,{
                    position:"absolute",
                    left:this.left+"px",
                    top:"-30px"
                })
            }
            parent.appendChild(this.snowImg);
            return this.snowImg;
        },
        update:function () {
            if(!this.snowImg)return;
            this.angle+=this.angleSpeed;
            this.alpha=this.alpha-this.alphaSpeed;
            if(this.alpha===0){
                this.dispose();
                return;
            }
            Object.assign(this.snowImg.style,{
                top:this.snowImg.offsetTop+this.speed+"px",
                transform:"rotate("+this.angle+"deg) scale("+this.scale+")",
                opacity:this.alpha
            });
            if(this.snowImg.offsetTop>document.documentElement.clientHeight){
                this.dispose();
            }
        },
        dispose:function () {
            if(!this.snowImg)return;
            this.snowImg.remove();
            this.snowImg=null;
        }
        
    };
    Snow.prototype.constructor=Snow;
    return Snow;
})();
var Main=(function (Snow) {
    function Main() {
        document.body.style.overflow="hidden";
        this.animationBind=this.animation.bind(this);
        this.animationBind();
    }
    Main.prototype={
        arr:[],
        times:5,
        animationBind:null,
        animation:function () {
            requestAnimationFrame(this.animationBind);
            this.randomTimeCreate();
            var arrs=[];
            for(var i=0;i<this.arr.length;i++){
                this.arr[i].update();
                if(this.arr[i].snowImg){
                    arrs.push(this.arr[i]);
                }else{
                    this.arr[i]=null;
                }
            }
            this.arr=arrs;
        },
        randomTimeCreate:function () {
            this.times--;
            if(this.times>0)return;
            this.times=Math.floor(Math.random()*20);
            this.createSnow();
        },
        createSnow:function () {
            var snow=new Snow(document.body);
            snow.scale=Math.random()*0.5+0.5;
            snow.angleSpeed=snow.angleSpeed*snow.scale;
            snow.alphaSpeed=snow.alphaSpeed*snow.scale;
            snow.speed=snow.speed*snow.scale;
            this.arr.push(snow);
        }
    };
    Main.prototype.constructor = Main;
    return Main;
})(Snow);

页面部分

    <script>
        new Main();
    </script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值