Js implements the method for controlling DIV movement by keyboard, and js keyboard div Movement
This article describes how to control DIV movement by using the keyboard in JavaScript. Share it with you for your reference. The specific analysis is as follows:
Css style section:
Copy codeThe Code is as follows: <style type = "text/css">
Html, body {overflow: hidden ;}
Body {margin: 0; padding: 0 ;}
Pre {color: green; padding: 10px 15px; background: # f0f0f0; border: 1px dotted #333; font: 12px/1.5 Courier New; margin: 12px ;}
Span {color: #999 ;}
# Box {position: absolute; top: 50px; left: 300px; width: 100px; height: 100px; background: red ;}
</Style>
Js section:
Copy codeThe Code is as follows: <script type = "text/javascript">
Window. onload = function ()
{
Var oBox = document. getElementById ("box ");
Var bLeft = bTop = bRight = bBottom = bCtrlKey = false;
SetInterval (function ()
{
If (bLeft)
{
OBox. style. left = oBox. offsetLeft-10 + "px"
}
Else if (bRight)
{
OBox. style. left = oBox. offsetLeft + 10 + "px"
}
If (bTop)
{
OBox. style. top = oBox. offsetTop-10 + "px"
}
Else if (bBottom)
{
OBox. style. top = oBox. offsetTop + 10 + "px"
}
// Prevent overflow
Limit ();
}, 30 );
Document. onkeydown = function (event)
{
Var event = event | window. event;
BCtrlKey = event. ctrlKey;
Switch (event. keyCode)
{
Case 37:
BLeft = true;
Break;
Case 38:
If (bCtrlKey)
{
Var oldWidth = oBox. offsetWidth;
Var oldHeight = oBox. offsetHeight;
OBox. style. width = oBox. offsetWidth * 1.5 + "px ";
OBox. style. height = oBox. offsetHeight * 1.5 + "px ";
OBox. style. left = oBox. offsetLeft-(oBox. offsetWidth-oldWidth)/2 + "px ";
OBox. style. top = oBox. offsetTop-(oBox. offsetHeight-oldHeight)/2 + "px ";
Break;
}
BTop = true;
Break;
Case 39:
BRight = true;
Break;
Case 40:
If (bCtrlKey)
{
Var oldWidth = oBox. offsetWidth;
Var oldHeight = oBox. offsetHeight;
OBox. style. width = oBox. offsetWidth * 0.75 + "px ";
OBox. style. height = oBox. offsetHeight * 0.75 + "px ";
OBox. style. left = oBox. offsetLeft-(oBox. offsetWidth-oldWidth)/2 + "px ";
OBox. style. top = oBox. offsetTop-(oBox. offsetHeight-oldHeight)/2 + "px ";
Break;
}
BBottom = true;
Break;
Case 49:
BCtrlKey & (oBox. style. background = "green ");
Break;
Case 50:
BCtrlKey & (oBox. style. background = "yellow ");
Break;
Case 51:
BCtrlKey & (oBox. style. background = "blue ");
Break;
}
Return false
};
Document. onkeyup = function (event)
{
Switch (event | window. event). keyCode)
{
Case 37:
BLeft = false;
Break;
Case 38:
BTop = false;
Break;
Case 39:
BRight = false;
Break;
Case 40:
BBottom = false;
Break;
}
};
// Prevent overflow
Function limit ()
{
Var doc = document.doc umentElement. clientWidth, document.doc umentElement. clientHeight]
// Prevent left Overflow
OBox. offsetLeft <= 0 & (oBox. style. left = 0 );
// Prevent top Overflow
OBox. offsetTop <= 0 & (oBox. style. top = 0 );
// Prevent overflow on the right
Doc [0]-oBox. offsetLeft-oBox. offsetWidth <= 0 & (oBox. style. left = doc [0]-oBox. offsetWidth + "px ");
// Prevents bottom Overflow
Doc [1]-oBox. offsetTop-oBox. offsetHeight <= 0 & (oBox. style. top = doc [1]-oBox. offsetHeight + "px ")
}
};
</Script>
Note:
Upper: lower left: lower right: →
Ctrl + 1: the background turns green
Ctrl + 2: the background turns yellow
Ctrl + 3: the background turns blue
Ctrl + ZOOM: Zoom in
Ctrl + ZOOM: Zoom out
Html section:
Copy codeCode: DIV]
<Div id = "box"> </div>
I hope this article will help you design javascript programs.