97国产精品视频人人做人人爱,3344在线观看无码,成年人国产视频,欧美日一级片,在线看AV天堂,高清无码一本到东京热,欧美一级黄片一区2区,免费又爽又刺激高潮网址

教你用面向對象編程寫一個煙花爆炸的

2020-3-23    前端達人

點擊查看原圖



想要學會這個漂亮的煙花嗎?快來跟著學習吧~

結構

<div class="container"></div>

我們只需要一個盒子表示煙花爆炸范圍就可以了

樣式

fire是煙花 注意添加絕對定位

 <style>
    .container{
        margin: 0 auto;
        height: 500px;
        width: 1200px;
        background: black;
        position: relative;
        overflow: hidden;
    }
    .fire{
        width: 10px;
        background: white;
        height: 10px;
        /* border-radius: 50%; */
        position: absolute;
        bottom: 0;
    }
    </style>



行為

編寫構造函數Firework

需要用到一個鼠標點擊的位置,一個div選擇器,一個爆炸樣式

 function Firework(x,y,selector,type){
        //此處獲取對象的方式為單例的思想,避免重復獲取相同的元素
        if(Firework.box && selector === Firework.box.selector){
            this.box =  Firework.box.ele;
        }else{
            Firework.box = {
                ele:document.querySelector(selector),
                selector:selector
            }
            this.box = Firework.box.ele;
        }
        this.type = type;
        this.init(x,y)
    }



封裝一個運動的方法
function animation(ele,attroptions,callback){
    for(var attr in attroptions){
        attroptions[attr] ={
            target:attroptions[attr],
            inow:parseInt(getComputedStyle(ele)[attr])
        } 
    }
    clearInterval(ele.timer);
    ele.timer = setInterval(function(){
        for(var attr in attroptions ){
            var item = attroptions[attr]
            var target = item.target;
            var inow = item.inow;
            var speed = (target - inow)/10;
            speed = speed>0?Math.ceil(speed):Math.floor(speed);
            if(Math.abs(target - inow) <= Math.abs(speed)){
                ele.style[attr] = target+"px";
                delete attroptions[attr];
                for(var num  in attroptions){
                    return false;
                }
                clearTimeout(ele.timer);
                if(typeof callback === "function")callback();
            }else{
                attroptions[attr].inow += speed;
                ele.style[attr]  = attroptions[attr].inow+"px";
            }
        }
    },30)
}



編寫原型方法
Firework.prototype = {
        constructor:Firework,
        //初始化
        init:function(x,y){
            //創建一個煙花
            this.ele = this.createFirework();
            //xy為鼠標落點
            this.x = x ;
            this.y = y;
            //maxXy為最大運動范圍
            this.maxX = this.box.offsetWidth - this.ele.offsetWidth;
            this.maxY = this.box.offsetHeight - this.ele.offsetHeight;
            //初始化結束后  煙花隨機顏色
            this.randomColor(this.ele);
            //煙花升空
            this.fireworkUp(this.ele);
        },
        //創造煙花
        createFirework:function(){
            var ele = document.createElement("div");
            ele.className = "fire";
            this.box.appendChild(ele);
            return ele;
        },
        //煙花升空
        fireworkUp:function(ele){
            ele.style.left = this.x + "px";
            //此處用到剛剛封裝的運動方法
            animation(ele,{top:this.y},function(){
                ele.remove();
                this.fireworkBlast()
            }.bind(this));
        },
        //煙花爆炸
        fireworkBlast:function(){
            for(var i = 0 ; i < 20; i++){
                var ele = document.createElement("div");
                ele.className = "fire";
                ele.style.left = this.x + "px";
                ele.style.top = this.y + "px";
                this.box.appendChild(ele);
                ele.style.borderRadius = "50%";
                this.randomColor(ele);
                //判定一下輸入的爆炸方式是原型煙花 還是散落煙花 由此更改獲取的煙花位置
                animation(ele,this.type === "circle"?this.circleBlast(i,20): this.randomPosition(),function(cale){
                    cale.remove();
                }.bind(this,ele))
            }
        },
        //圓形爆炸位置
        circleBlast:function(i,total){
            var r = 200;
            var reg = 360 / total *i;
            var deg = Math.PI / 180 *reg;
            return {
                left:r * Math.cos(deg) + this.x ,
                top:r * Math.sin(deg) + this.y 
            }
        },
        //隨機顏色
        randomPosition:function(){
            return {
                left : Math.random()*this.maxX,
                top : Math.random()*this.maxY
            }
        },
        randomColor:function(ele){
            var color =  "#" + parseInt(parseInt("ffffff",16)*Math.random()).toString(16).padStart(6,0);
            return ele.style.backgroundColor = color;
        }
    }



綁定事件
document.querySelector(".container").addEventListener("click",function(evt){
    var e = evt||event;
    new Firework(e.offsetX,e.offsetY,".container","circle")
    new Firework(e.offsetX,e.offsetY,".container")
})

全部代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    .container{
        margin: 0 auto;
        height: 500px;
        width: 1200px;
        background: black;
        position: relative;
        overflow: hidden;
    }
    .fire{
        width: 10px;
        background: white;
        height: 10px;
        /* border-radius: 50%; */
        position: absolute;
        bottom: 0;
    }
    </style>
</head>
<body>
    <div class="container"></div>
    <script src="./utils.js"></script>
    <script>

    function animation(ele,attroptions,callback){
        for(var attr in attroptions){
            attroptions[attr] ={
                target:attroptions[attr],
                inow:parseInt(getComputedStyle(ele)[attr])
            } 
        }
        clearInterval(ele.timer);
        ele.timer = setInterval(function(){
            for(var attr in attroptions ){
                var item = attroptions[attr]
                var target = item.target;
                var inow = item.inow;
                var speed = (target - inow)/10;
                speed = speed>0?Math.ceil(speed):Math.floor(speed);
                if(Math.abs(target - inow) <= Math.abs(speed)){
                    ele.style[attr] = target+"px";
                    delete attroptions[attr];
                    for(var num  in attroptions){
                        return false;
                    }
                    clearTimeout(ele.timer);
                    if(typeof callback === "function")callback();
                }else{
                    attroptions[attr].inow += speed;
                    ele.style[attr]  = attroptions[attr].inow+"px";
                }
            }
        },30)
    }  

        function Firework(x,y,selector,type){
            if(Firework.box && selector === Firework.box.selector){
                this.box =  Firework.box.ele;
            }else{
                Firework.box = {
                    ele:document.querySelector(selector),
                    selector:selector
                }
                this.box = Firework.box.ele;
            }
            this.type = type;
            this.init(x,y)
        }

        Firework.prototype = {
            constructor:Firework,
            //初始化
            init:function(x,y){
                this.ele = this.createFirework();
                this.x = x ;
                this.y = y;
                this.maxX = this.box.offsetWidth - this.ele.offsetWidth;
                this.maxY = this.box.offsetHeight - this.ele.offsetHeight;
                this.randomColor(this.ele);
                this.fireworkUp(this.ele);
            },
            //創造煙花
            createFirework:function(){
                var ele = document.createElement("div");
                ele.className = "fire";
                this.box.appendChild(ele);
                return ele;
            },
            fireworkUp:function(ele){
                ele.style.left = this.x + "px";
                animation(ele,{top:this.y},function(){
                    ele.remove();
                    this.fireworkBlast()
                }.bind(this));
            },
            //煙花爆炸
            fireworkBlast:function(){
                for(var i = 0 ; i < 20; i++){
                    var ele = document.createElement("div");
                    ele.className = "fire";
                    ele.style.left = this.x + "px";
                    ele.style.top = this.y + "px";
                    this.box.appendChild(ele);
                    ele.style.borderRadius = "50%";
                    this.randomColor(ele);
                    animation(ele,this.type === "circle"?this.circleBlast(i,20): this.randomPosition(),function(cale){
                        cale.remove();
                    }.bind(this,ele))
                }
            },
            circleBlast:function(i,total){
                var r = 200;
                var reg = 360 / total *i;
                var deg = Math.PI / 180 *reg;
                return {
                    left:r * Math.cos(deg) + this.x ,
                    top:r * Math.sin(deg) + this.y 
                }
            },
            randomPosition:function(){
                return {
                    left : Math.random()*this.maxX,
                    top : Math.random()*this.maxY
                }
            },
            randomColor:function(ele){
                var color =  "#" + parseInt(parseInt("ffffff",16)*Math.random()).toString(16).padStart(6,0);
                return ele.style.backgroundColor = color;
            }
        }

        document.querySelector(".container").addEventListener("click",function(evt){
            var e = evt||event;
            new Firework(e.offsetX,e.offsetY,".container","circle")
            new Firework(e.offsetX,e.offsetY,".container")
        })
    </script>
</body>
</html>

————————————————
版權聲明:本文為CSDN博主「SpongeBooob」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_41383900/article/details/105026768


日歷

鏈接

個人資料

藍藍設計的小編 http://www.0391cbd.com

存檔

主站蜘蛛池模板: 亚洲欧洲日韩久久狠狠爱| 精品欧美一区二区三区久久久| 欧美成人精品在线| 99青青青精品视频在线| 国产精品久久精品| 精品99在线观看| 国产精品护士| 国产精品网址在线观看你懂的| 国产在线一二三区| 久久国产精品电影| 久久久精品久久久久三级| 亚洲人成网线在线播放va| 久久亚洲国产一区二区| 中文字幕人成人乱码亚洲电影| 久久网欧美| 免费亚洲成人| 亚洲免费毛片| 国产99精品视频| 国产成人AV男人的天堂| 亚洲综合激情另类专区| 久久中文无码精品| 国产丝袜一区二区三区视频免下载| 手机精品福利在线观看| 一级爆乳无码av| 国产成人无码播放| 毛片免费试看| 欧美国产精品拍自| 国产一级精品毛片基地| 亚洲va在线观看| 久久久久九九精品影院| 污污网站在线观看| 欧美在线天堂| 伊人色在线视频| 亚洲中文在线视频| 成人免费网站在线观看| 亚洲视频四区| 无码国内精品人妻少妇蜜桃视频| 日本精品中文字幕在线不卡| 全部无卡免费的毛片在线看| 亚洲欧美综合另类图片小说区| 免费jjzz在在线播放国产| 国产Av无码精品色午夜| 亚洲精品爱草草视频在线| 久久香蕉国产线看精品| 亚洲日韩精品综合在线一区二区| 亚洲人在线| 黄色网站在线观看无码| 欧美精品色视频| 国产99热| 亚洲国产精品无码AV| 激情无码字幕综合| 99热这里只有精品5| 无遮挡国产高潮视频免费观看| 2020亚洲精品无码| 亚洲三级色| 国产免费观看av大片的网站| 国产精品第三页在线看| 亚洲国产精品久久久久秋霞影院| 亚洲无线观看| 国产哺乳奶水91在线播放| 亚洲永久色| 久久96热在精品国产高清| 日韩欧美国产另类| 日韩中文字幕亚洲无线码| 国产日韩欧美视频| 丁香婷婷在线视频| 中文成人在线视频| 98超碰在线观看| 欧美日韩成人在线观看| 97色伦色在线综合视频| 扒开粉嫩的小缝隙喷白浆视频| 啪啪啪亚洲无码| 国产精品冒白浆免费视频| 免费一级毛片在线播放傲雪网 | 国产成人免费手机在线观看视频 | 日本人又色又爽的视频| 深爱婷婷激情网| 天天色综网| 久久五月视频| 久夜色精品国产噜噜| 国产成人啪视频一区二区三区| 亚洲av日韩av制服丝袜|