HTML DOM Style animation Property


CSS allows us to animate transitions of elements’ properties. We use the animation property to define our desired styles. We can combine properties like animation-name, animation-duration, animation-iteration-count, etc. using the animation keyword.

Syntax

The syntax of animation property is as follows −

object.style.animation = "name duration timingFunction delay iterationCount direction fillMode playState"

Values

Following are the values −

ValueDescription
animation-nameFor specifying the keyframe name to bind the selector to.
animation-durationTo specifiy the time duration of the animation(in seconds or milliseconds) for completion.
animation-timing-functionTo specify the animation speed curve.
animation-delayTo specify some delay before the animation starts
animation-iteration-countTo specify the no of time an animation should be played
animation-directionTo indicate if the animation should or should not play in the alternate or reverse cycle.
animation-fill-modeTo specify the values that are applied by the animation outside the time it is executing
animation-play-stateTo specify if the animation is currently paused or playing.
initialFor setting this property to initial value.
inheritTo inherit the parent property value.

Example

Let us look at the example for the animation property −

Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   div {
      width: 5px;
      height: 15px;
      background-color: limegreen;
      animation: demo 4s infinite;
   }
   @keyframes demo {
      from {width: 5px; background-color: limegreen;}
      to {width: 400px; background-color: darkgreen;}
   }
   @keyframes demo1 {
      from {height: 5px; background-color: limegreen;}
      to {height: 400px; background-color: darkgreen;}
   }
</style>
<script>
   function changeAnimation() {
      document.getElementById("DIV1").style.animation = "demo1 4s 2";
   }
</script>
</head>
<body>
<button onclick="changeAnimation()">CHANGE ANIMATION</button>
<p>Change the below animation by clicking the above button</p>
<div id="DIV1"></div>
</body>
</html>

Output

This will produce the following output −

On clicking the CHANGE ANIMATION button, the animation will change −

Updated on: 15-Feb-2021

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements