Code has been added to clipboard!

Using JavaScript Animation in a Nutshell With Real Examples

Reading time 2 min
Published Mar 27, 2019
Updated Oct 2, 2019

Whenever you create a new website, you want to capture the attention of your users, keep them engaged, and provide them with a great experience. A great way to do that is making the code you write in JavaScript animate your pages.

JavaScript animations can be of various kinds - a content box sliding in upon page load, some hilarious text animation, anything you can imagine. However, overstacking your website with JS animations might make it look tacky.

In this tutorial, you will learn to create a simple JavaScript DOM animation. We will also go through aspects of HTML and CSS to explain how to set up a proper layout.

JavaScript Animation: Main Tips

  • To create an HTML animation JavaScript can be used.
  • Container elements must have a relative position, and JS animation elements must have an absolute position.

Code Examples

Let's write our initial code for what will eventually be a JavaScript animation.

Example
<strong>An animation of JavaScript</strong>
<div id="animate">The location of the animation</div>

Now, we must have some place to put our creation in. Using the code below, we will position it within a Javascript animation container:

Example
<div id="contain">
    <div id="animation">The location of the animation</div>
</div>

Let's now set the container element to position: relative and the animation JavaScript element to position: absolute:

Example
#container {
  width: 500px;
  height: 500px;
  position: relative;
  background: green;
}
#animate {
  width: 60px;
  height: 60px;
  position: absolute;
  background: red;
}

An element's style is changed by programming movement step by step. A timer calls out those changes. Continuous JavaScript animations are achieved by setting a tiny timer interval using setInterval:

Example
var id = setInterval(frame, 6);

function frame() {
    if (/* test */) {
        clearInterval(id);
    } else {
        /* element style changes */  
    }
}

Create JS Animations

We have covered every step, and you should have grasped the basic ideas by now. Of course, to strengthen this new knowledge, you should try to put it to practice. Use the code snippets provided, get inspired, and create your first JavaScript animation examples on your own:

Example
function myMove() {
    var el = document.getElementById("animate"); 
    var loc = 0;
    var ids = setInterval(frame, 6);
    function frame() {
        if (loc == 350) {
            clearInterval(ids);
        } else {
            loc++; 
            el.style.top = loc + 'px'; 
            el.style.left = loc + 'px'; 
        }
    }
}

JavaScript Animation: Summary

  • You can use JavaScript to create HTML animations.
  • It is important to pay attention to and set the right value to the position property.
  • You can use style to style the elements of your JavaScript animations in any way you like.
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen