Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HTML | DOM animationiteration Event

  • Last Updated : 24 Jul, 2019

The animationiteration event occurs when a CSS animation is repeated.

The animation will only be played one-time if the CSS animation-iteration-count property is set to “1”. The animation needs to run more than once for this event to fire.

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

Events that can occur when a CSS animation plays:

  • animationstart: occurs when the CSS animation has started.
  • animationiteration: occurs when the CSS animation is repeated.
  • animationend: occurs when the CSS animation has completed.

Syntax:



  • Code for Chrome, Safari and Opera
    object.addEventListener("webkitAnimationIteration", myScript);
  • Standard syntax:
    object.addEventListener("animationiteration", myScript);

Example:




<!DOCTYPE html>
<html>
  
<head>
    <style>
        #div {
            width: 100%;
            height: 100px;
            background: green;
            position: relative;
            font-size: 40px;
        }
        
        /* Chrome, Safari, Opera */
        @-webkit-keyframes mymove {
            from {
                top: 0px;
            }
            to {
                top: 200px;
            }
        }
          
        @keyframes mymove {
            from {
                top: 0px;
            }
            to {
                top: 200px;
            }
        }
    </style>
</head>
  
<body>
    <center>
        <h1 style="color:green">
          GeeksforGeeks
      </h1>
  
        <div id="div" onclick="GFGFun()">
            Click me to start the animation.
        </div>
  
        <script>
            var x = document.getElementById("div");
  
            // Start the animation with JavaScript
            function GFGFun() {
                // Code for Chrome, Safari and Opera
                x.style.WebkitAnimation = "mymove 4s 2";
                // Standard syntax
                x.style.animation = "mymove 4s 2";
            }
  
            // Code for Chrome, Safari and Opera
            x.addEventListener("webkitAnimationStart", StartFun);
            x.addEventListener("webkitAnimationIteration", RepeatFun);
  
            // Standard syntax
            x.addEventListener("animationstart", StartFun);
            x.addEventListener("animationiteration", RepeatFun);
  
            function StartFun() {
                this.innerHTML = "The animation has started";
                this.style.backgroundColor = "lime";
            }
  
            function RepeatFun() {
                this.innerHTML = "Animation was played again";
                this.style.backgroundColor = "darkgreen";
            }
        </script>
    </center>
</body>
  
</html>

Output:

Supported Browsers: The browsers supported by DOM animationiteration Event are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :