JS实现用键盘控制DIV

JS实现用键盘控制DIV,第1张

概述&lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt&lthtml xmlns="http://www.w3.or <!DOCTYPE HTML PUBliC "-//W3C//DTD xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<head>
<Meta http-equiv="Content-Type" content="text/HTML; charset=utf⑻" />
<Title>用键盘控制div</Title>
<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>
<script type="text/@R_404_3715@"> 
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"
}
//避免溢出
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;
}
};
 
//避免溢出
function limit()
{
var doc = [document.documentElement.clIEntWIDth,document.documentElement.clIEntHeight]
//避免左边溢出
oBox.offsetleft <=0 && (oBox.style.left = 0);
//避免顶部溢出
oBox.offsettop <=0 && (oBox.style.top = 0);
//避免右边溢出
doc[0] - oBox.offsetleft - oBox.offsetWIDth <= 0 && (oBox.style.left = doc[0] - oBox.offsetWIDth + "px");
//避免底部溢出
doc[1] - oBox.offsettop - oBox.offsetHeight <= 0 && (oBox.style.top = doc[1] - oBox.offsetHeight + "px")
}
};
</script>
</head>
<body>
<pre>
红色方块为键盘 *** 作区域,您可以进行以下 *** 作:
上:↑ 下:↓ 左:← 右:→
Ctrl + 1 : 背景变成绿色
Ctrl + 2 : 背景变成黄色
Ctrl + 3 : 背景变成蓝色
Ctrl + ↑ : 放大
Ctrl + ↓ : 缩小
</pre>
<div ID="Box"></div>
<div><A href="http://www.999jiujiu.com/">http://www.999jiujiu.com/</A></div>
</body>
</HTML> 总结

以上是内存溢出为你收集整理的JS实现用键盘控制DIV全部内容,希望文章能够帮你解决JS实现用键盘控制DIV所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1017409.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存