bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/DOM and Browser APIs
JavaScript•DOM and Browser APIs

HTML DOM Animation

Learn to create HTML animations using JavaScript.

A Basic Web Page

To demonstrate how to create HTML animations with JavaScript, we will use a simple web page:

Example

<!DOCTYPE html>
<html>
<body>
<h1>My First
 JavaScript Animation</h1>
<div id="animation">My animation will go here</div>
</body>
</html>

Create an Animation Container

All animations should be relative to a container element.

Example

<div id ="container">
 <div id ="animate">My
 animation will go here</div>
</div>

Style the Elements

The container element should be created with style = " position: relative ".

The animation element should be created with style = " position: absolute ".

Example

Formatted code
#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}
#animate {
  width: 50px;
  height: 50px;
  position: absolute;
  background: red;
}

Live preview

Animation Code

JavaScript animations are done by programming gradual changes in an element's style.

The changes are called by a timer. When the timer interval is small, the animation looks continuous.

Example

id = setInterval(frame, 5);
function frame() {
 if (/* test for finished */) {
 clearInterval(id);
 } else {
 /* code to change the element style */
}
}

Create the Full Animation Using JavaScript

Example

function myMove() {
  let id = null;
  const elem = document.getElementById("animate");
  let pos = 0;
  clearInterval(id);
  id = setInterval(frame, 5);
  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
    pos++;
    elem.style.top = pos + 'px';
    elem.style.left = pos + 'px';
  }
}
}

Previous

HTML Form Validation

Next

HTML DOM Document

This chapter

Overview
16

Lessons

47m

Read time

1. The HTML DOM2. HTML DOM API3. Selecting DOM Elements4. HTML DOM - Changing HTML5. HTML DOM - Changing CSS6. HTML Form Validation7. HTML DOM Animation8. HTML DOM Document9. JavaScript Events10. JavaScript Mouse Events11. JavaScript Keyboard Events12. JavaScript Load Events13. JavaScript Timing Events14. JavaScript Event Management15. JavaScript HTML DOM Events16. JavaScript EventListener

On this page

A Basic Web PageCreate an Animation ContainerStyle the ElementsAnimation CodeCreate the Full Animation Using JavaScript