asdw键移动图片.html

<body>
<div id="myImg" style="position: absolute; width: 100px;height: 100px;background-color: pink;border: red solid 2px; border-radius: 20px;">
<!--<img style='width: 200px;' src="img/Clocks.gif">-->
</div>

<script>
var myImg = document.getElementById('myImg');
document.onkeydown = keyDown;

function keyDown(event){
switch (event.keyCode){
case 87:
myImg.style.top = myImg.offsetTop - 5+'px';
break;
case 83:
myImg.style.top = myImg.offsetTop + 5+'px';
break;
case 65:
myImg.style.left = myImg.offsetLeft - 5+'px';
break;
case 68:
myImg.style.left = myImg.offsetLeft + 5+'px';
break;
default:
break;
}
}

</script>

</body>

posted @ 2020-05-11 12:03  阿向向  阅读(105)  评论(0编辑  收藏  举报