方向键控制对象匀速运动问题

seachers 2011-08-03 08:26:22
<body>
<div id="desktop" style="border:1px solid #000; width:500px; height:500px; margin:0 auto; position:relative;">
<div id="object" style="position:absolute; top:10px; left:10px; width:10px; height:10px; background-color:#F00;"></div>
</div>
<script type="text/javascript">
var timer1,timer2,timer3,timer4;
var direction = null;
function xia(){
if(direction == "xia"){return false; }
document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)+ 10) + "px";
if(parseInt(document.getElementById("object").style.top) > 490){
document.getElementById("object").style.top = 490 + "px";
}
clearTimeout(timer2);
clearTimeout(timer3);
clearTimeout(timer4);
timer1 = setTimeout("xia()",5000);
direction = "xia";
}
function shang(){
if(direction == 'shang') return false;
direction = 'shang';
document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)- 10) + "px";
if(parseInt(document.getElementById("object").style.top) <0){
document.getElementById("object").style.top = 0 + "px";
}
}
function zuo(){
if(direction == 'zuo') return false;
direction = 'zuo';
document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)- 10) + "px";
if(parseInt(document.getElementById("object").style.left) <0){
document.getElementById("object").style.left = 0 + "px";
}
}
function you(){
if(direction == 'you') return false;
direction = 'you';
document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)+ 10) + "px";
if(parseInt(document.getElementById("object").style.left) > 490){
document.getElementById("object").style.left = 490 + "px";
}
}
document.onkeydown = function(e){
var tops = document.getElementById("object").style.top;
var e = e || window.event;
switch(e.keyCode){
case 40:
xia();
break;
case 38:
clearInterval(timer1);
clearInterval(timer3);
clearInterval(timer4);
timer2 = setInterval("shang()",500);
break;
case 37:
clearInterval(timer2);
clearInterval(timer1);
clearInterval(timer4);
timer3 = setInterval("zuo()",500);
break;
case 39:
clearInterval(timer2);
clearInterval(timer3);
clearInterval(timer1);
timer4 = setInterval("you()",500);
break;
default:
document.write("I'm looking forward to this weekend!")

}
};
</script>

</body>完整代码如上,请高手指点。怎么样才能实现点键盘上的方向键,小方块匀速远动,连续点击小方块不加速呢?
...全文
113 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
seachers 2011-08-03
  • 打赏
  • 举报
回复
谢谢你的回答。
hongmei85 2011-08-03
  • 打赏
  • 举报
回复
这样?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> new document </title>
<meta name="keywords" content="">
<meta name="description" content="">
</head>

<body>
<div id="desktop" style="border:1px solid #000; width:500px; height:500px; margin:0 auto; position:relative;">
<div id="object" style="position:absolute; top:10px; left:10px; width:10px; height:10px; background-color:#F00;"></div>
</div>
<script type="text/javascript">
var timer1,timer2,timer3,timer4;
var direction = null;
function xia(){
if(direction != "xia")
direction = "xia";
document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)+ 10) + "px";
if(parseInt(document.getElementById("object").style.top) > 490){
document.getElementById("object").style.top = 490 + "px";
}


}
function shang(){
if(direction != 'shang')
direction = 'shang';
document.getElementById("object").style.top = (parseInt(document.getElementById("object").style.top)- 10) + "px";
if(parseInt(document.getElementById("object").style.top) <0){
document.getElementById("object").style.top = 0 + "px";
}
}
function zuo(){
if(direction != 'zuo')
direction = 'zuo';
document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)- 10) + "px";
if(parseInt(document.getElementById("object").style.left) <0){
document.getElementById("object").style.left = 0 + "px";
}
}
function you(){
if(direction != 'you')
direction = 'you';
document.getElementById("object").style.left = (parseInt(document.getElementById("object").style.left)+ 10) + "px";
if(parseInt(document.getElementById("object").style.left) > 490){
document.getElementById("object").style.left = 490 + "px";
}
}
document.onkeydown = function(e){
var tops = document.getElementById("object").style.top;
var e = e || window.event;
switch(e.keyCode){
case 40:
clearInterval(timer1);
clearInterval(timer2);
clearInterval(timer3);
clearInterval(timer4);
timer1 = setInterval("xia()",500);
break;
case 38:
clearInterval(timer1);
clearInterval(timer2);
clearInterval(timer3);
clearInterval(timer4);
timer2 = setInterval("shang()",500);
break;
case 37:
clearInterval(timer1);
clearInterval(timer2);
clearInterval(timer3);
clearInterval(timer4);
timer3 = setInterval("zuo()",500);
break;
case 39:
clearInterval(timer1);
clearInterval(timer2);
clearInterval(timer3);
clearInterval(timer4);
timer4 = setInterval("you()",500);
break;
default:
document.write("I'm looking forward to this weekend!")

}
};
</script>

</body>
</html>
  • 打赏
  • 举报
回复


<div id="container" style="top:0;left:0;position:absolute;background-color:#eee;width:100px;height:100px;"></div>

<script type="text/javascript">

//<![CDATA[

var EventUtility = {

addHandler: function (element, eventName, handler) {
if (typeof element.addEventListener === "function")
element.addEventListener(eventName, handler, false);
else if (typeof element.attachEvent === "object")
element.attachEvent("on" + eventName, handler);
else
element["on" + eventName] = handler;
}

};

function handler(event) {
var rate = 20;
var keyCode = event.keyCode;
var container = document.getElementById("container");
switch (keyCode) {
case 37: container.style.left = (parseInt(container.style.left, 10) - rate) + "px"; break;
case 39: container.style.left = (parseInt(container.style.left, 10) + rate) + "px"; break;
case 38: container.style.top = (parseInt(container.style.top, 10) - rate) + "px"; break;
case 40: container.style.top = (parseInt(container.style.top, 10) + rate) + "px"; break;
default: break;
}
}

EventUtility.addHandler(document.documentElement,"keydown",handler);

//]]>

</script>


没事眯一会 2011-08-03
  • 打赏
  • 举报
回复
我也想知道,帮顶~

87,989

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧