JavaScript-在當前顯示區範圍內實現點不到的小方塊

來源:互聯網
上載者:User

標籤:round   rto   pos   doctype   asc   and   com   存在   oct   

 1 <!doctype html> 2 <html> 3  <head> 4     <meta charset="UTF-8"> 5     <title>在當前顯示區範圍內實現點不到的小方塊</title> 6     <style> 7         div{position:fixed;width:50px;height:50px; 8             background-color:pink; 9         }10     </style>11 12  </head>13  <body>14  <div id="pop"></div>15     <script>16         var game={17             PSIZE:0,//儲存div的大小18             MAXTOP:0,//儲存最大可以top19             MAXLEFT:0,//儲存最大可用left20             pop:null,//儲存主角div21             init:function(){22                 //尋找id為pop的div儲存在pop屬性中23                 this.pop=document.getElementById("pop");24                 //擷取pop計算後的樣式中的width,轉為浮點數儲存在PSIZE屬性中25                 this.PSIZE=parseFloat(getComputedStyle(this.pop).width);26                 //計算MAXTOP:文檔顯示區的高-PSIZE27                 this.MAXTOP=innerHeight-this.PSIZE;28                 //計算MAXLEFT:文檔顯示區的寬-PSIZE29                 this.MAXLEFT=innerWidth-this.PSIZE;30                 //debugger;31                 debugger;32                 //在0-MAXTOP之間產生隨機數,儲存在變數rTop中33                 var rTop=Math.floor(Math.random()*(this.MAXTOP+1));34                 //在0-MAXLEFT之間產生隨機數,儲存在變數rLeft中35                 var rLeft=Math.floor(Math.random()*(this.MAXLEFT+1));36                 //設定pop的top為rTop37                 this.pop.style.top=rTop+"px";38                 //設定pop的left為rLeft39                 this.pop.style.left=rLeft+"px";40             }41 42         }43         game.init();44     </script>45 46  </body>47 </html>

 

JavaScript-在當前顯示區範圍內實現點不到的小方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.