永发信息网

怎么获取键盘的两个keyCode值?

答案:2  悬赏:20  手机版
解决时间 2021-01-30 19:36
  • 提问者网友:溺爱和你
  • 2021-01-30 04:21
怎么获取键盘的两个keyCode值? 意思就是说 我按W+A是左上移动 按住S+D是右下 下面的代码只是一个按W 上 S下 A 左 if ((event.keyCode==65)&& (event.keyCode==87)){document.getElementById("fish").style.pixelTop=document.getElementById("fish").style.pixelTop-10;document.getElementById("fish").style.pixelLeft=document.getElementById("fish").style.pixelLeft-10;} 为什么我这样写却不起作用- - 很郁闷呀游泳的鱼 function move( ) { if (event.keyCode==87)  document.getElementById("fish").style.pixelTop=document.getElementById("fish").style.pixelTop-10;  if (event.keyCode==83)  document.getElementById("fish").style.pixelTop=document.getElementById("fish").style.pixelTop+10;  if (event.keyCode==65)  document.getElementById("fish").style.pixelLeft=document.getElementById("fish").style.pixelLeft-10;  if (event.keyCode==68)  document.getElementById("fish").style.pixelLeft=document.getElementById("fish").style.pixelLeft+10; }document.onkeydown= move ;
最佳答案
  • 五星知识达人网友:几近狂妄
  • 2021-01-30 05:54
&lt;HTML&gt;<br>&lt;HEAD&gt;<br>&lt;META http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot;&gt;<br>&lt;TITLE&gt;通过键盘移动图像&lt;/TITLE&gt;<br>&lt;STYLE type=&quot;text/css&quot;&gt;<br>&lt;!--<br>body {<br> background-image: url(images/game.jpg);<br> background-repeat: repeat-y;<br>}<br><br>--&gt;<br>&lt;/STYLE&gt;<br>&lt;SCRIPT language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;<br> function move( )<br> {<br>  alert(event.keyCode);<br> if (event.keyCode==38)<br>    document.getElementById(&quot;man&quot;).style.pixelTop=document.getElementById<br><br>(&quot;man&quot;).style.pixelTop-5;<br>  if (event.keyCode==40)<br>    document.getElementById(&quot;man&quot;).style.pixelTop=document.getElementById<br><br>(&quot;man&quot;).style.pixelTop+5;<br>  if (event.keyCode==37)<br>    document.getElementById(&quot;man&quot;).style.pixelLeft=document.getElementById<br><br>(&quot;man&quot;).style.pixelLeft-5;<br>  if (event.keyCode==39)<br>    document.getElementById(&quot;man&quot;).style.pixelLeft=document.getElementById<br><br>(&quot;man&quot;).style.pixelLeft+5;<br> }<br>document.onkeydown= move ;<br> &lt;/SCRIPT&gt;<br>&lt;/HEAD&gt;<br><br>&lt;BODY&gt;<br>&lt;DIV id=&quot;man&quot; style=&quot;position:absolute;left:543px; top:288px;width:89px;height:115px;z-<br><br>index:1&quot;&gt;&lt;IMG src=&quot;images/man.gif&quot; width=&quot;82&quot; height=&quot;152&quot;&gt;&lt;/DIV&gt;<br>&lt;/BODY&gt;<br>&lt;/HTML&gt;
全部回答
  • 1楼网友:煞尾
  • 2021-01-30 06:32

<html>

<head>

<title> js实现图片移动

</title>

<script language="javascript">

<!--

function init()

{

block=blockdiv.style

block.xpos=parseint(block.left)

block.ypos=parseint(block.top)

block.activeleft=false

block.activeright=false

block.activeup=false

block.activedown=false

document.onkeydown=keydown

document.onkeyup=keyup

}

function keydown(e) {

{var iekey=event.keycode; var nkey=0}

if (( iekey==65) && !block.activeleft)

{block.activeleft=true

block.activeright=false

slideleft()

}

if (( iekey==68) && !block.activeright)

{

block.activeright=true

block.activeleft=false

slideright()

}

if(( iekey==87)&& !block.activeup)

{

block.activeup=true

block.activedown=false

slideup()

}

if ((iekey==83) && !block.activedown)

{

block.activedown=true

block.activeup=false

slidedown()

}

}

function keyup(e) {

 {var iekey=event.keycode;}

if( iekey==65) block.activeleft=false

if( iekey==68) block.activeright=false

if( iekey==87) block.activeup=false

if( iekey==83) block.activedown =false

}

function slideleft() {

if (block.activeleft){

block.xpos-=10

block.left=block.xpos

status="x:"+ block.xpos+"y:"+ block.ypos

settimeout("slideleft()",20)

}

}

function slideright(){

if(block.activeright){

block.xpos+=10

block.left=block.xpos

status="x:"+block.xpos+"y:" + block.ypos

settimeout("slideright()",20)

}

}

function slideup() {

if(block.activeup){

block.ypos-=10

block.top=block.ypos

status="x:"+block.xpos +"y:"+block.ypos

settimeout("slideup()",20)

}

}

function slidedown() {

if (block.activedown) {

block.ypos+=10

block.top=block.ypos

status="x:"+block.xpos+"y:" + block.ypos

settimeout("slidedown()",20)

}

}

//-->

</script>

</head>

<body onload="init()">

<p>

<br>2 向下,4向左,6向右,8向上

</p>

<div id="blockdiv" style="position:absolute;left:50;top:85;width:40;">

<img src="suzh1.jpg" width=40 height=40 border=0>

</div>

</body>

</html>

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯