本文实例讲述了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程序设计有所帮助。