我有这个代码,我试图让其中一个div动画。 div应该向下移动,然后降低20%后应该开始向上移动,然后再向下移动,等等。Javascript的setInterval()只运行一次函数
问题是,看起来,代码只运行一次。这意味着div只下跌2%,然后停留在那个位置。
的Javascript
var head = document.getElementById('char_furuta_head');
function anime() {
var direction = 1; /* 1 = down, -1 = up */
setInterval(frame, 1000);
function frame() {
str = head.style.top;
str = substring(0,str.length);
var lel = parseInt(str);
console.log(lel);
if (direction == 1) {
lel += 2;
head.style.top = lel + '%';
if(lel == 20) { direction = -1; };
} else {
lel -= 2;
head.style.top = lel + '%';
if(lel == 0) { direction = 1; };
}
}
}
它只有一次运行的可能性较小,而且更有可能您的逻辑只会导致页面发生重大更改一次。 –
console.log()是你的朋友...用它来看看会发生什么... – epascarello