I have this code, where I'm trying to animate one of the divs. The div should move down, then after getting 20% lower should start moving up, then again down and so on.
The problem is, as it seems, that the code runs only once. Which means the div moves down by 2% only once and then stays at that position.
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; };
}
}
}