【发布时间】:2021-08-23 20:05:10
【问题描述】:
我对 javascript 还很陌生,一直在尝试弄清楚如何使图像移动。我想出了这段代码,它有点工作,但图像在一点点后停止移动。我该如何解决这个问题?
<!DOCTYPE html>
<html>
<head>
<style>
#img {
position: relative;
}
</style>
<title>This is filler text</title>
</head>
<body>
<p>This is filler text</p>
<img src="example.jpeg" length="100" width="100" id="img" />
<script>
document.onkeydown = function(event) {
switch (event.keyCode) {
case 37:
moveLeft();
break;
case 38:
moveUp();
break;
case 39:
moveRight();
break;
case 40:
moveDown();
break;
}
};
function moveLeft() {
document.getElementById("img").style.left += "5px";
}
function moveRight(){
document.getElementById("img").style.right += "5px";
}
function moveDown(){
document.getElementById("img").style.bottom += "5px";
function moveUp(){
document.getElementById("img").style.top += "5px";
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript html