【发布时间】:2019-07-15 07:41:57
【问题描述】:
我正在尝试使用我的代码使用 JavaScript 更改图像位置,但由于某种原因它不起作用。谁能解释一下原因。
var walk, isWaveSpawned = true;
var walkers = [];
function start()
{
walk = document.getElementById("walk");
draw(); //Animation function
}
function draw()
{
if(isWaveSpawned) //Generate a wave of 5 "walkers"
{
isWaveSpawned = false;
for(var i = 0; i < 5; i++
walkers.push(new createWalker());
}
for(var o = 0; o < walkers.length; o++) //Add 1px to x position after each frame
{
walkers[o].x += walkers[o].speed;
walkers[o].image.style.left = walkers[o].x;
walkers[o].image.style.top = walkers[o].y;
}
requestAnimationFrame(draw);
}
function createWalker()
{
this.x = 0;
this.y = 100;
this.speed = 1;
this.image = walk.cloneNode(false); //Possible cause of issue
}
<!DOCTYPE html>
<html>
<body onload="start()">
<img id="walk" src="https://i.imgur.com/ArYIIjU.gif">
</body>
</html>
我的 GIF 图片在左上角可见但没有移动。
附:添加了一个 HTML/JS sn-p,但它输出了一些错误,而我最终没有看到这些错误。
【问题讨论】:
-
您能否编辑您的问题并使用 stackoverflow 的 HTML/CSS/Javascript Snippet 而不是粘贴文件内容,以便我们轻松尝试和重现您的代码。
标签: javascript html animation gif