Animation and positioning
Animating things is fun. There’s something about creating something that just.. moves on its own that is quite satisfying. If we want to get to some of the interesting, gamey type stuff in HTML, we need to figure out two things: 1) How to create code that runs more than once at a time. and 2) how to position elements at set places on the screen.
Creating code that runs multiple time a second - setInterval
The javascript-level keyword, setInterval, will setup your project to run on a specific interval, based on miliseconds.
Making DOM elements that can be moved around
In order to animate any of the elements on the screen, they must have their positions set to either absolute, or relative. While these are CSS properties, we will be specifying them via javascript in these examples.
Positioning DOM elements
In order to move a DOM element around, one typically will adjust the element’s top and left style properties. Top will move the element around on the ‘y’/vertical axis, while left will move the object around on the ‘x’/horizontal axis.
Moving DOM elements
Combining our setInterval code with positioning code will get us actual animation. This is done by creating variables whose value we modify with each call of the animation tick, and then changing an element’s position on the page based on those numbers. All of these variables have to be outside the function’s scope.