js实现键盘的上下左右控制图片移动

<body OnLoad="setOB();focus()">

<script language="JavaScript">

<!-- var key=0 var xx

function setOB(){

n = (document.layers) ? 1 : 0; X = (n)? document.ppkoabcd : ppkoabcd.style Xpos = parseInt(X.left);

Ypos = parseInt(X.top);

document.onkeydown = keyDown;

document.onkeyup = keyUp;

if (n) document.captureEvents(Event.keydown | Event.keyup);

}

function keyDown(e) {

key = (n)? e.which : event.keyCode if (key == 108 || key == 37) m(1,2);

if (key == 114 || key == 39) m(1,3);

if (key == 100 || key == 40) m(1,4);

if (key == 117 || key == 38) m(1,5);

}

function keyUp(e) {

key=0;clearTimeout(xx);

}

function m(t,u) {

clearTimeout(xx) if (t==1){ if (u==2){X.left = Xpos-=5;

xx = setTimeout("m(1,2)", 40);

}

if (u==3){

X.left = Xpos+=5;xx = setTimeout("m(1,3)", 40);

}

if (u==4){

X.top = Ypos+=5;xx = setTimeout("m(1,4)", 40);

}

if (u==5){

X.top = Ypos-=5;xx = setTimeout("m(1,5)", 40);

}

   } } //-->

</script>

<div id="ppkoabcd" >   <img src="webgame/0.gif"> </div>

posted @ 2014-11-03 16:46  小细雨  阅读(3215)  评论(1编辑  收藏  举报