• <center id="sm46c"></center>
  • <dfn id="sm46c"></dfn>
  • <strike id="sm46c"></strike>
  • <cite id="sm46c"><source id="sm46c"></source></cite>
    • <strike id="sm46c"><source id="sm46c"></source></strike>
      <option id="sm46c"></option>
      国产精品天天看天天狠,女高中生强奷系列在线播放,久久无码免费的a毛片大全,国产日韩综合av在线,亚洲国产中文综合专区在,特殊重囗味sm在线观看无码,中文字幕一区二区三区四区在线,无码任你躁久久久久久老妇蜜桃

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

      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.li-bodun.cn

      存檔

      主站蜘蛛池模板: 精品亚洲成a人片在线观看| 亚洲成A人片在线观看的电影| 日韩一区精品视频一区二区| 亚洲国产香蕉碰碰人人| 少妇愉情理伦片丰满丰满午夜| 亚洲欧美日韩国产国产a| 18禁男女爽爽爽午夜网站免费| 久久久久人妻一区精品色欧美| 国产精品自拍午夜福利| 出租屋勾搭老熟妇啪啪| 日本视频高清一区二区三区| 欧美日韩中文国产一区发布| 成人精品视频99在线观看免费| 人人人妻人人人妻人人人| 成人网站免费观看永久视频下载 | 日韩内射美女人妻一区二区三区| av一区二区中文字幕| 久久精品国产99国产精品导航| 99久久综合九九亚洲| 日本在线欧美在线| 在线日韩日本国产亚洲| 亚洲一区二区三区日本久久九| 四虎成人永久在线精品免费| 麻豆精品久久久久久久99蜜桃| 国产av无码一区二区二三区j| 亚洲av成人一区国产精品| 成人无码视频| 丰满爆乳无码一区二区三区| 久久伊人五月丁香狠狠色| 一本久道久久综合五月丁香| 久久精品亚洲专区| 亚洲综合久久一本伊一区| 狠狠噜天天噜日日噜| 亚洲愉拍自拍另类图片| 国产国产久热这里只有精品| 亚洲高潮喷水中文字幕| 无码人妻丰满熟妇区毛片18| 4hu四虎永久在线观看| 国产精品国产精品偷麻豆| 国产午夜精品av一区二区| 欧美人成视频在线视频|