- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - DOM Animation
The DOM animation can be achieved by changing the DOM element's style using JavaScript. When changes are gradual and time interval is small, the animation looks continuous. Generally, there are three ways to animate a DOM element:
Using CSS transitions − It utilizes pre-defined animation styles in CSS triggered by changes in the element's properties.
Using CSS animations − It offers more control over animation timing and behavior by defining keyframes and animation properties within the CSS file.
Using JavaScript − It provides the most flexibility, allowing you to dynamically manipulate style properties and create complex animations directly within your JavaScript code.
This chapter provides a basic understanding of how to animate DOM elements using JavaScript.
Animate DOM Elements with JavaScript
JavaScript can be used to change the style of the DOM element.
You change the style of the DOM element after a particular time frame to animate them. For example, you can use the setInterval() method to change the position of the DOM element to move it from one position to another with animation.
Similarly, you can update CSS properties like ‘animation’, etc., to animate the element dynamically.
Furthermore, the requestAnimationFrame() method can also be used to animate the DOM elements.
Below, you will learn different ways to animate the DOM elements.
Animate DOM elements using setInterval() method
You can invoke a setInterval() method after each time frame and change the style of the DOM element to animate them. However, you can keep the time frame small to run animation smoothly.
Syntax
Follow the syntax below to use the setInterval() method to animate DOM elements.
let id = setInterval(frame_func, timeframe); function frame_func() { if (animation_end) { clearInterval(id); } else { // change style to animate } }
In the above syntax, we start the animation using the setInterval() method and call the frame_func() after every ‘timeframe’ milliseconds.
In the frame_func() function, we have defined the condition to end or continue the animation.
Example
In the below code, we have styled the <div> elements.
When users click the button, it calls the startAnimation() function.
In the startAnimation() function, we have defined the ‘pos’ variable and initialized it with 0, representing the initial position of the div element.
After that, we used the setInterval() method to invoke the animationFrame() function after every 5 milliseconds.
In the animationFrame() function, if the position of the inner div becomes 350, we stop the animation using the clearInterval() method. Otherwise, we change the left position of the inner div.
When you click the button, it will move the inner div element from left to right.
<!DOCTYPE html> <html> <head> <style> #parent { width: 700px; height: 50px; position: relative; background: yellow; } #child { width: 50px; height: 50px; position: absolute; background-color: red; } </style> </head> <body> <div id = "parent"> <div id = "child"> </div> </div> <br> <button onclick = "startAnimation()"> Animate Div </button> <script> function startAnimation() { const elem = document.getElementById("child"); // Starting position let pos = 0; // Changing frames for animation let id = setInterval(animationFrame, 5); function animationFrame() { // Stop the animation if (pos == 350) { clearInterval(id); } else { pos++; elem.style.left = pos + "px"; } } } </script> </body> </html>
Example
In the below code, the background color of the <div> element is green.
We use the setInterval() method to call the animationFrame() function after every 50 milliseconds.
In the animationFrame() function, we change the opacity of the <div> element by 0.1. We stop the animation when the opacity becomes less than or equal to 0 using the clearInterval() method.
<!DOCTYPE html> <html> <head> <style> #parent { width: 700px; height: 200px; background: green; } </style> </head> <body> <div id = "parent"> </div> <br> <button onclick = "startAnimation()"> Animate Div </button> <script> function startAnimation() { const parent = document.getElementById("parent"); let opacity = 1; let id = setInterval(animationFrame, 50); function animationFrame() { if (opacity <= 0) { // Stop animation clearInterval(id); parent.style.opacity = 1; parent.style.backgroundColor = "red"; } else { // Decrease the opacity parent.style.opacity = opacity; opacity = opacity - 0.1; } } } </script> </body> </html>
Animate DOM elements using requestAnimationFrame() method
The requestAnimationFrame() method is used to animate the DOM elements like the setInterval() method. It executes the tasks continuously and repaints the next frame in the browser.
The requestAnimationFrame() method makes rendering more efficient than the setInterval() method.
Syntax
Follow the syntax below to use the requestAnimationFrame() method to animate DOM elements.
function animate() { // Animation logic // Request the next animation frame requestAnimationFrame(animate); } // Animation loop animate();
Let’s understand how the requestAnimationFrame() method works.
You pass the callback function as an argument of the requestAnimationFrame() method to execute the next frame.
The web browser will execute the callback before repainting the next frame.
The callback function will update the DOM element.
The browser will repaint the DOM element.
Again, the browser will call the callback function, and the loop will continue.
You can use the cancelAnimationFrame() method to cancel animation.
Example
In the code below, we have defined the startAnimation() and stopAnimation() functions and invoked them when the user clicks the button.
In the startAnimation() function, we increment the value of the ‘pos’ by 1, and update the left position of the child div element.
After that, we used the requestAnimationFrame() method to paint the next frame in the web browser. It will move the child div element from left to right in the parent div element.
The stopAnimation() function uses the cancelAnimationFrame() method to stop the animation. It takes the id returned by the requestAnimationFrame() method as an argument.
<!DOCTYPE html> <html> <head> <style> #parent {width: 700px; height: 50px; position: relative;background: yellow;} #child {width: 50px;height: 50px; position: absolute; background-color: red;} </style> </head> <body> <div id = "parent"> <div id = "child"> </div> </div> <br> <button onclick = "startAnimation()"> Animate Div </button> <button onclick = "stopAnimation()"> Stop Animation </button> <script> let animationId; let pos = 0; function startAnimation() { const elem = document.getElementById("child"); function animationFrame() { if (pos < 650) { pos++; elem.style.left = pos + "px"; // Make a call for a next frame animationId = requestAnimationFrame(animationFrame); } } // Start Animation animationFrame(); } function stopAnimation() { // Stop animation if (animationId) { cancelAnimationFrame(animationId); animationId = null; } } </script> </body> </html>
Animate DOM Elements by changing the CSS Properties
The ‘animation’ property of the CSS can be used to add animation to the DOM element. JavaScript also allows the customization of the ‘animation’ property.
Syntax
Follow the syntax below to animate the DOM element by changing the value of the ‘animation’ property of the element in JavaScript.
element.style.animation = "key_frame_name duration timing_function iterationCount";
Property values
key_frame_name − It is the name of the keyframe, which you need to define in the CSS.
duration − It is the duration of the animation.
timing_function − It is used to set how animation should be executed.
iterationCount − It specifies how many times the animation should repeat.
Example
In the below code, when users click the button, we call the animateDiv() function and update the value of the ‘animation’ property of the style object of the div element.
We have already defined the ‘moveAnimation’ keyframe in CSS. So, when you click the button it will start moving the div element.
<!DOCTYPE html> <html> <head> <style> #element { width: 90px; height: 90px; background: blue; color: white; position: relative; text-align: center; } @keyframes moveAnimation { from { transform: translateX(0); } to { transform: translateX(550px); } } </style> </head> <body> <div id = "element"> Animate </div> <br> <button onclick = "animateDiv()"> Animate Div </button> <script> function animateDiv() { const element = document.getElementById("element"); element.style.animation = "moveAnimation 3s ease-in-out infinite"; } </script> </body> </html>
The best way to animate the DOM element is using the requestAnimationFrame() method, which animates the DOM element smoothly. Also, it can be used to execute different animations simultaneously.