Animation is the process of creating movement or visual effects on web pages by continuously changing the properties of HTML elements over a period of time.
JavaScript provides several ways to create animations:
- CSS Animations
- JavaScript Animation Libraries
- requestAnimationFrame()
- setInterval() and setTimeout()
Animations are widely used to create:
- Sliding menus
- Loading indicators
- Moving objects
- Image sliders
- Interactive games
- Modern UI effects
CSS animations allow developers to animate HTML elements without writing JavaScript.
<style>
@keyframes slideIn {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0);
}
}
.animated{
animation: slideIn 1s ease-in-out;
}
</style>
<div class="animated">
This is an animated element.
</div>@keyframes
Defines animation stages.
fromStarting position.
toEnding position.
animationApplies the animation to the element.
The element moves smoothly from the left side to its original position.
JavaScript libraries simplify animation development.
Popular Libraries:
- GSAP (GreenSock)
- Anime.js
- Velocity.js
Example using GSAP:
gsap.to(".animated",
{
duration:1,
x:100,
ease:"power1.inOut"
});- duration → Animation duration
- x → Move horizontally by 100px
- ease → Controls speed curve
requestAnimationFrame() is a built-in browser method used for smooth animations.
It synchronizes animation with the browser refresh rate.
requestAnimationFrame(functionName)function animate(){
const element =
document.querySelector(".animated");
let position = 0;
function frame(){
position++;
element.style.transform =
`translateX(${position}px)`;
if(position < 100){
requestAnimationFrame(frame);
}
}
requestAnimationFrame(frame);
}
animate();Step 1:
let position = 0;Stores current position.
Step 2:
position++;Increases position.
Step 3:
element.style.transformMoves the element.
Step 4:
requestAnimationFrame(frame)Requests the browser to render the next frame.
The element moves smoothly from left to right.
JavaScript also allows animations using:
setInterval()This repeatedly executes a function after a fixed interval.
setInterval(function, milliseconds)Example:
setInterval(fun,1000)Calls:
fun()Every:
1000 milliseconds
=
1 second
<!DOCTYPE html>
<html>
<head>
<title>Animation</title>
<style>
#box{
background-color:gray;
width:350px;
height:350px;
position:relative;
}
#circle{
background-color:yellowgreen;
width:50px;
height:50px;
border-radius:40%;
position:absolute;
}
</style>
</head>
<body>
<h1>Animation</h1>
<button onclick="ani()">
Click
</button>
<div id="box">
<div id="circle"></div>
</div>
<script>
function ani(){
var c =
document.getElementById("circle");
var c1 = 0;
var id =
setInterval(Animate,5);
function Animate(){
if(c1 == 300){
clearInterval(id);
}
else{
c1++;
c.style.top = c1 + "px";
c.style.left = c1 + "px";
}
}
}
</script>
</body>
</html>function ani()Creates an animation function.
This function runs when:
<button onclick="ani()">is clicked.
var c =
document.getElementById("circle");Selects:
<div id="circle">from HTML.
var c1 = 0;Stores the current position.
Initially:
c1 = 0
setInterval(Animate,5);Calls:
Animate()every:
5 milliseconds
if(c1 == 300)Checks whether the circle reaches:
300 pixels
clearInterval(id)Stops the animation.
c1++;Increases the position.
Example:
0
1
2
3
4
.....
300
c.style.top =
c1 + "px";Moves vertically.
c.style.left =
c1 + "px";Moves horizontally.
(0,0)
↓
(50,50)
↓
(100,100)
↓
(150,150)
↓
(300,300)
The circle moves diagonally.
clearInterval(id)Purpose:
Stops the timer created by:
setInterval()Example:
var id =
setInterval(fun,1000);
clearInterval(id);The timer stops immediately.
document.getElementById("circle")Returns:
HTML element object
Moves an element vertically.
Example:
element.style.top="100px"Moves an element horizontally.
Example:
element.style.left="100px"Two CSS properties are important here:
#box{
position:relative;
}Creates a reference container.
#circle{
position:absolute;
}Allows the circle to move inside the box.
When the page loads:
+----------------------+
Yellow Circle
+----------------------+
When user clicks:
Button Click
↓
Circle moves
↘
Diagonally
↓
Stops at
(300px,300px)
Answer:
Animation is the process of changing an element's properties continuously to create movement or visual effects.
Answer:
- CSS Animation
- requestAnimationFrame()
- setInterval()
- setTimeout()
- Animation Libraries
Answer:
It requests the browser to execute an animation before the next repaint for smoother animations.
Answer:
It repeatedly executes a function after a fixed time interval.
Syntax:
setInterval(function,milliseconds)Answer:
It stops the timer created using:
setInterval()Answer:
It allows the element to move freely inside its parent container.
Answer:
document.getElementById()Answer:
style.top
style.left- Animations create visual movement.
- CSS animations are simple and fast.
- requestAnimationFrame() provides smooth animations.
- setInterval() repeatedly executes functions.
- clearInterval() stops animations.
- position:absolute is used for movable elements.
- style.top changes vertical position.
- style.left changes horizontal position.
Animations in JavaScript make web applications interactive and visually appealing. Developers can create animations using CSS, JavaScript libraries, requestAnimationFrame(), or timer functions like setInterval(). The combination of DOM manipulation and animation techniques enables developers to build modern user interfaces, games, loaders, sliders, and dynamic web applications efficiently.