破釜沉舟论坛 » WEB 开发 » js:键盘方向键移动图片

2007-8-10 16:15 php店小二
js:键盘方向键移动图片

[code]<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
</head>
<body onkeydown="move()">
<script>
function move(){
        ek=event.keyCode;
        if (ek==37) myArea.style.posLeft-=5;
        if (ek==39) myArea.style.posLeft+=5;
        if (ek==38) myArea.style.posTop-=5;
        if (ek==40) myArea.style.posTop+=5;
}
</script>
<h2 align=center>鍵盤事件:使用方向鍵來移動物件</h2>
<hr>
<div id=myArea style="position:absolute; top:20; left:20; height:50; width:50; background-color:#00FF00"></div>
請按鍵盤的方向鍵來移動綠色的方塊。
<hr>
</body>
</html>[/code]

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.