頁面上有一個div 100*100 顏色為黃色 按鍵盤上下左右鍵可以操作div移動 ctrl+鍵盤上下鍵可以放大縮小 ctrl+鍵盤左右鍵可以隨機變顏色

來源:互聯網
上載者:User

標籤:pos   鍵盤   doc   listen   bre   rip   eve   bsp   idt   

 1 <body> 2 <div id="w"> 3     <div class="n">按上鍵可以向上移動</div> 4     <div class="n">按下鍵可以向下移動</div> 5     <div class="n">按左鍵可以向左移動</div> 6     <div class="n">按右鍵可以向右移動</div> 7     <div class="n">ctrl+鍵盤上鍵可以放大</div> 8     <div class="n">ctrl+鍵盤下鍵可以縮小</div> 9     <div class="n">ctrl+鍵盤左右鍵可以隨機變顏色</div>10     <div id="box" style="width: 100px;height: 100px;background-color: #ff0;position: absolute;left: 100px;top: 100px; z-index: -1"></div>11 </div>12 <script>13     var box = document.getElementById(‘box‘);14     document.body.addEventListener(‘keydown‘, function (e) {15         switch (e.keyCode) {16             case 37:17                 if (e.ctrlKey) {18                     box.style.backgroundColor = ‘#‘ + (Math.random().toString(16)).substr(-6);19                 } else {20                     box.style.left = parseInt(box.style.left) - 6 + ‘px‘;21                 }22                 break;23             case 39:24                 if (e.ctrlKey) {25                     box.style.backgroundColor = ‘#‘ + (Math.random().toString(16)).substr(-6);26                 } else {27                     box.style.left = parseInt(box.style.left) + 6 + ‘px‘;28                 }29                 break;30             case 38:31                 box.style.top = parseInt(box.style.top) - 6 + ‘px‘;32                 if (e.ctrlKey) {33                     box.style.width = parseInt(box.style.width) + 4 + ‘px‘;34                     box.style.left = parseInt(box.style.left) - 2 + ‘px‘;35                     box.style.height = parseInt(box.style.height) + 4 + ‘px‘;36                     box.style.top = parseInt(box.style.top) + 4 + ‘px‘;37                 }38                 break;39             case 40:40                 box.style.top = parseInt(box.style.top) + 6 + ‘px‘;41                 if (e.ctrlKey) {42                     box.style.width = parseInt(box.style.width) - 4 + ‘px‘;43                     box.style.left = parseInt(box.style.left) + 2 + ‘px‘;44                     box.style.height = parseInt(box.style.height) - 4 + ‘px‘;45                     box.style.top = parseInt(box.style.top) - 4 + ‘px‘;46                 }47                 break;48         }49         console.log(e.keyCode);50     })51 </script>52 </body>

 

頁面上有一個div 100*100 顏色為黃色 按鍵盤上下左右鍵可以操作div移動 ctrl+鍵盤上下鍵可以放大縮小 ctrl+鍵盤左右鍵可以隨機變顏色

聯繫我們

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