<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>通过键盘移动图像</TITLE>
<STYLE type="text/css">
body {
background-image: url(images/game.jpg);
background-repeat: repeat-y;
}
</STYLE>
<script type="text/javascript">
var advman = 0;
function init()
{
advman = document.getElementById("man").style.pixelTop;
}
function move(event)
{
e = event||window.event;
var obj = document.getElementById("man");
if(e.keyCode==38)//up
obj.style.top=obj.offsetTop-20+"px";
if(e.keyCode==40)//down
obj.style.top=obj.offsetTop+20+"px";
if(e.keyCode==37)//left
obj.style.left=obj.offsetLeft-20+"px";
if(e.keyCode==39)//right
obj.style.left=obj.offsetLeft+20+"px";
}
document.οnkeydοwn= move ;
</script>
</HEAD>
<BODY>
<DIV id="man" style="position:absolute;left:500px;top:300px;width:100px;height:100px;z-index:1;background:red;"></DIV>
</BODY>
</HTML>
js按键移动div
最新推荐文章于 2020-12-20 08:52:41 发布