一個JS的小練習

來源:互聯網
上載者:User

標籤:

目的是讓一個DIV小格子在頁面中跑起來並且變色

 1  <style> 2         * { 3             margin: 0px; 4             padding: 0px; 5         } 6  7         #DIV1 { 8             position: relative; 9             width: 50px;10             height: 50px;11             background-color: red;12         }13     </style>14 </head>15 <body>16     <form id="form1" runat="server">17         <div id="DIV1"></div>18     </form>19 </body>20 </html>21 <script>22     var C = new Array();//定義一個數組放置顏色23     C[0] = ‘green‘;24     C[1] = ‘yellow‘;25     C[2] = ‘black‘;26     C[3] = ‘navy‘;27     C[4] = ‘blue‘;28     C[5] = ‘red‘;29     var num = 0;//設定一個變數記錄顏色變化的次數以及取顏色在數組中所對應的索引30     var num1 = 0;//設定幾個變數記錄函數的執行次數31     var div = document.getElementById(‘DIV1‘);//擷取DIV32     function F() {33         if (num1 % 10 == 0) {//控制顏色的變化速度34             div.style.backgroundColor = C[num];//改變顏色35             num++;36             if (num == C.length) {37                 num = 0;//顏色變化次數達到數組長度時歸零重新記錄38             }39         }40         div.style.left = div.offsetLeft + 10 + ‘px‘;//DIV橫向移動10個像素41         if (div.offsetLeft > 1300) {//移動1300像素時換行加橫向位置歸零42             div.style.left = 0 + ‘px‘;43             div.style.top = div.offsetTop + 50 + ‘px‘;44         }45         if (div.offsetTop > 500) {//縱向達到500時DIV回到原點46             div.style.left = 0 + ‘px‘;47             div.style.top = 0 + ‘px‘;48         }49         num1++;50     }51     window.setInterval(‘F()‘, 20);//每20毫秒執行一次函數52 </script>

 

一個JS的小練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.