本文實例講述了js實作鍵盤控制DIV移動的方法。分享給大家供大家參考。具體分析如下: css樣式部分: 複製程式碼 程式碼如下: <br /> html,body{overflow:hidden;}<br /> body{margin:0;padding:0;}<br /> pre{color:green;padding:10px 15px;background:#f0f0f0;border:1px dotted #333;font:12px/1.5 Courier New;margin:12px;}<br /> span{color:#999;}<br /> #box{position:absolute;top:50px;left:300px;width:100px;height:100px;background:red;}<br /> js部分: 複製程式碼 程式碼如下: <br /> window.onload = function ()<br /> {<br /> var oBox = document.getElementById("box");<br /> var bLeft = bTop = bRight = bBottom = bCtrlKey = false; <p>setInterval(function ()<br /> {<br /> if (bLeft)<br /> {<br /> oBox.style.left = oBox.offsetLeft - 10 "px"<br /> }<br /> else if (bRight)<br /> {<br /> oBox.style.left = oBox.offsetLeft 10 "px"<br /> } <p>if (bTop)<br /> {<br /> oBox.style.top = oBox.offsetTop - 10 "px" <br /> }<br /> else if(bBottom)<br /> {<br /> oBox.style.top = oBox.offsetTop 10 "px"<br /> }<br /> //防止溢流<br /> limit();<br /> },30); <p>document.onkeydown = function (event)<br /> {<br /> var event = event || window.event;<br /> bCtrlKey = event.ctrlKey; <p>switch (event.keyCode)<br /> {<br /> case 37:<br /> bLeft = true;<br /> break;<br /> case 38:<br /> if(bCtrlKey)<br /> {<br /> var oldWidth = oBox.offsetWidth;<br /> var oldHeight = oBox.offsetHeight; <p>oBox.style.width = oBox.offsetWidth * 1.5 "px";<br /> oBox.style.height = oBox.offsetHeight * 1.5 "px"; <p>oBox.style.left = oBox.offsetLeft - (oBox.offsetWidth - oldWidth) / 2 "px";<br /> oBox.style.top = oBox.offsetTop - (oBox.offsetHeight - oldHeight) / 2 "px"; <p>break; <br /> }<br /> bTop = true;<br /> break;<br /> case 39:<br /> bRight = true;<br /> break;<br /> case 40:<br /> if(bCtrlKey)<br /> {<br /> var oldWidth = oBox.offsetWidth;<br /> var oldHeight = oBox.offsetHeight; <p>oBox.style.width = oBox.offsetWidth * 0.75 "px";<br /> oBox.style.height = oBox.offsetHeight * 0.75 "px"; <p>oBox.style.left = oBox.offsetLeft - (oBox.offsetWidth - oldWidth) / 2 "px";<br /> oBox.style.top = oBox.offsetTop - (oBox.offsetHeight - oldHeight) / 2 "px"; <p>break; <br /> }<br /> bBottom = true;<br /> break;<br /> case 49:<br /> bCtrlKey && (oBox.style.background = "green");<br /> break;<br /> case 50:<br /> bCtrlKey && (oBox.style.background = "yellow");<br /> break;<br /> case 51:<br /> bCtrlKey && (oBox.style.background = "blue");<br /> break;<br /> } <p>return false<br /> }; <p>document.onkeyup = function (event)<br /> {<br /> switch ((event || window.event).keyCode)<br /> {<br /> case 37:<br /> bLeft = false;<br /> break;<br /> case 38:<br /> bTop = false;<br /> break;<br /> case 39:<br /> bRight = false;<br /> break;<br /> case 40:<br /> bBottom = false;<br /> break;<br /> }<br /> }; <p>//防止溢流<br /> function limit()<br /> {<br /> var doc = [document.documentElement.clientWidth, document.documentElement.clientHeight]<br /> //防止左側溢位<br /> oBox.offsetLeft <=0 && (oBox.style.left = 0);<br /> //防止頂部溢出<br /> oBox.offsetTop <=0 && (oBox.style.top = 0);<br /> //防止右側溢流<br /> doc[0] - oBox.offsetLeft - oBox.offsetWidth <= 0 && (oBox.style.left = doc[0] - oBox.offsetWidth "px");<br /> //防止底部溢出<br /> doc[1] - oBox.offsetTop - oBox.offsetHeight <= 0 && (oBox.style.top = doc[1] - oBox.offsetHeight "px")<br /> }<br /> };<br /> 說明: 上:↑ 下:↓ 左:← 右:→ Ctrl 1 : 背景變成綠色 Ctrl 2 : 背景變成黃 Ctrl 3 : 背景變成藍色 Ctrl ↑ : 放大 Ctrl ↓ : 縮小 html部分: 複製程式碼 代碼如下: 移動的方塊【DIV】 希望本文所述對大家的javascript程式設計有所幫助。