AnimationEvent elapsedTime Property - Javascript DOM

Javascript examples for DOM:Animation Event

Description

The elapsedTime property returns how long an animation started in seconds.

This property is read-only.

Return Value

A Number, representing the number of seconds an animation has been running

The following code shows how to find out how many seconds an animation has been running:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {/*  w ww  .  j a va2 s. c o  m*/
    margin: 25px;
    width: 550px;
    height: 100px;
    background: orange;
    position: relative;
    font-size: 20px;
    text-align: center;
    -webkit-animation: mymove 4s infinite; /* Chrome, Safari, Opera */
    animation: mymove 4s infinite;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes mymove {
    from {top: 0px;}
    to {top: 200px;}
}

@keyframes mymove {
    from {top: 0px;}
    to {top: 200px;}
}
</style>
</head>
<body>

<div id="myDIV">The animation has started</div>

<script>
var x = document.getElementById("myDIV");

// Code for Chrome, Safari and Opera
x.addEventListener("webkitAnimationIteration", myRepeatFunction);

// Standard syntax
x.addEventListener("animationiteration", myRepeatFunction);

function myRepeatFunction(event) {
    this.style.backgroundColor = "lightblue";
    this.innerHTML = "Elapsed time: " + event.elapsedTime + " seconds";
}
</script>

</body>
</html>

Related Tutorials