First animation in JS
Hi! I just did the lection in the JS course where we see an animation of a box moving to the right within another box. Wanting to experiment a bit more I tried to create an animation so that the box would then move down, and to the left and then up again and then just round and round in "circles". It works fine until it's time to move left (and then up), so I was wondering if someone had any tips for how I can fix this issue?:) I've pasted the JS-code in below and the project can also be found on my profile, thanks for any help!:) /calling the function in window.onload to make sure the HTML is loaded window.onload = function() { var pos = 0; //our box element var box = document.getElementById('box'); var t = setInterval(move, 10); var toTheRight = true var down = false var toTheLeft = false var up = false function move() { if(pos >= 150 && toTheRight) { //clearInterval(t); toTheRight = false pos = 0 down = true } if(pos >= 150 && down) { pos = 0 down = false toTheLeft = true } if (pos>=150 && toTheLeft) { pos = 0 toTheLeft= false up=true } if (pos>=150 && up) { pos = 0 up = false toTheRight = true } if (toTheRight) { pos += 1; box.style.left = pos+'px'; } if (down) { pos += 1; box.style.top = pos+'px' } if (toTheLeft) { console.log("Hei") pos += 1; box.style.right = pos + 'px' } if (up) { pos+=1; box.style.bottom = pos + 'px' } } };