怎么获取键盘的两个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 ;
<HTML><br><HEAD><br><META http-equiv="Content-Type" content="text/html; charset=gb2312"><br><TITLE>通过键盘移动图像</TITLE><br><STYLE type="text/css"><br><!--<br>body {<br> background-image: url(images/game.jpg);<br> background-repeat: repeat-y;<br>}<br><br>--><br></STYLE><br><SCRIPT language="javascript" type="text/javascript"><br> function move( )<br> {<br> alert(event.keyCode);<br> if (event.keyCode==38)<br> document.getElementById("man").style.pixelTop=document.getElementById<br><br>("man").style.pixelTop-5;<br> if (event.keyCode==40)<br> document.getElementById("man").style.pixelTop=document.getElementById<br><br>("man").style.pixelTop+5;<br> if (event.keyCode==37)<br> document.getElementById("man").style.pixelLeft=document.getElementById<br><br>("man").style.pixelLeft-5;<br> if (event.keyCode==39)<br> document.getElementById("man").style.pixelLeft=document.getElementById<br><br>("man").style.pixelLeft+5;<br> }<br>document.onkeydown= move ;<br> </SCRIPT><br></HEAD><br><br><BODY><br><DIV id="man" style="position:absolute;left:543px; top:288px;width:89px;height:115px;z-<br><br>index:1"><IMG src="images/man.gif" width="82" height="152"></DIV><br></BODY><br></HTML>
<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>
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息