您的位置:首页 > Web前端 > JavaScript

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

2014-11-03 16:46 766 查看
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: