HTML | DOM AnimationEvent

In the HTML document, AnimationEvent interface is used to represent events providing information related to animations. AnimationEvent contains properties such as animationName, elapsedTime and pseudoElement.

  1. animationName: The animationName property returns the name of the event animation. It returns the value of the animation-name CSS property associated with the transition.
    Syntax:

    event.animationName;
    

    Return Type: It returns a string which represents the name of the animation.

    Example-1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <style>
            div {
                width: 550px;
                height: 50px;
                background: green;
                position: relative;
                -webkit-animation-name: animate;
                /* Chrome, Safari, Opera */
                -webkit-animation-duration: 5s;
                /* Chrome, Safari, Opera */
                animation-name: animate;
                animation-duration: 5s;
            }
            /* Chrome, Safari, Opera */
              
            @-webkit-keyframes animate {
                from {
                    top: 0px;
                }
                to {
                    top: 500px;
                }
            }
              
            @keyframes animate {
                from {
                    top: 0px;
                }
                to {
                    top: 500px;
                }
            }
        </style>
    </head>
      
    <body>
        <div id="div1"></div>
        <script>
            var x = document.getElementById("div1");
            
            // Code for Chrome, Safari and Opera
            x.addEventListener(
              "webkitAnimationStart", StartAnimation);
            
            // Standard syntax
            x.addEventListener(
              "animationstart", StartAnimation);
      
            
            // Return animation name
            function StartAnimation(event) {
                this.innerHTML =
                  "The animation-name is:"
                + event.animationName;
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Output:

  2. elapsedTime: The elapsedTime property returns the number of seconds for which an animation has been running in an animation event.

    Syntax:

    event.elapsedTime;
    

    Return Type: It returns a number which represents the number of seconds for which an animation has been running.

    Example-2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <style>
            div {
                width: 50px;
                height: 50px;
                background: green;
                position: relative;
                /* Chrome, Safari, Opera */
               -webkit-animation: animate 4s infinite;
                animation: animate 4s infinite;
            }
            /* Chrome, Safari, Opera */
              
            @-webkit-keyframes animate {
                from {
                    left: 500px;
                }
                to {
                    left: 0px;
                }
            }
              
            @keyframes animate {
                from {
                    left: 500px;
                }
                to {
                    left: 0px;
                }
            }
        </style>
    </head>
      
    <body>
        <div id="div1"></div>
        
        <script>
            var x = document.getElementById("div1");
            
            // Code for Chrome, Safari and Opera
            x.addEventListener(
           "webkitAnimationIteration", RepeatAnimation);
            
            // Standard syntax
            x.addEventListener(
            "animationiteration", RepeatAnimation);
      
            function RepeatAnimation(event) {
      
                this.style.backgroundColor = 
                  "lightgreen";
                this.innerHTML = "Elapsed time: "
                + event.elapsedTime + " seconds";
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Output:

Supported Browsers:

  • Google Chrome
  • Mozilla Firefox
  • Edge
  • Safari
  • Opera


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.