<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style >
#box1{
width: 100px;
height: 100px;
background-color: yellowgreen;
position: absolute;
}
</style>
<script type="text/javascript">
window.onload=function () {
document.onkeydown=function (event) {
event=event||window.event;
switch (event.keyCode) {
case 37:
box1.style.left=box1.offsetLeft-10+"px";
break;
case 39:
box1.style.left=box1.offsetLeft+10+"px";
break;
case 38:
box1.style.top=box1.offsetTop-10+"px";
break;
case 40:
box1.style.top=box1.offsetTop+10+"px";
break;
}
}
}
</script>
</head>
<body>
<div id="box1"></div>
</body>
</html>
使用上下左右进行移动