[英]Javascript animation with setInterval doesn't work
I'm creating a simple animation with a comet goes down. 我正在创建一个彗星掉落的简单动画。
Here is the comet itself: 这是彗星本身:
var cometScene = function(){
var b = document.createElement('div');
b.id = 'cometio';
var cometImage = document.createElement('img');
cometImage.setAttribute('src', 'images/comet1.png');
b.appendChild(cometImage);
document.getElementById('wrap').appendChild(b);
}
And here is the move: 这是移动:
function cometMove(){
var comet = document.getElementById('cometio');
var pos = 0;
var interval = setInterval(scene, 3);
function scene(){
if (pos === 1000){
clearInterval(interval);
} else {
pos++;
comet.style.top = pos + 'px';
comet.style.left = pos + 'px';
}
}
}
But the problem is it is only happens once and then I have to refresh the page to run an animation again. 但是问题是它只会发生一次,然后我必须刷新页面才能再次运行动画。 What am I doing wrong here?
我在这里做错了什么?
I have to refresh the page to run an animation again
我必须刷新页面才能再次运行动画
if (pos === 1000){
clearInterval(interval); /* you have stopped animation here */
}
To re-run or keep running, possibly you may just reset the position of comet: 要重新运行或继续运行,可能只需重置彗星的位置即可:
if (pos === 1000){
pos = 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.