JavaScript——用键盘wasd控制div移动

来源:互联网 发布:php 分班问题 编辑:程序博客网 时间:2024/06/07 13:48

w:上    a:左   s:下  d:右

<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="div" style="width: 100px;height: 100px;position: absolute;left: 100px;top: 100px;border: 1px solid red;background-                 color: red;"></div>
<script type="text/javascript">
var div=document.getElementById('div');
document.onkeydown=function(e){
switch(e.keyCode){
case 65:
div.style.left=parseInt(div.style.left)-10+"px";
break;
case 68:
div.style.left=parseInt(div.style.left)+10+"px";
break;
case 83:
div.style.top=parseInt(div.style.top)+10+"px";
break;
case 87:
div.style.top=parseInt(div.style.top)-10+"px";
break;
}
}
</script>
</body>
</html>

0 0
原创粉丝点击