animationstart Event - Javascript DOM

Javascript examples for DOM:Event

Description

The animationstart event occurs when a CSS animation starts.

Summary

Bubbles Yes
Cancelable No
Event type: AnimationEvent
DOM Version: Level 3 Events

The following code shows how to handle animationstart Event.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {/*  w  w  w . j  a  v  a2 s .co  m*/
    margin: 25px;
    width: 550px;
    height: 100px;
    background: orange;
    position: relative;
    font-size: 20px;
}

/* 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" onclick="myFunction()">Click me to start the animation.</div>

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

// Start the animation with JavaScript
function myFunction() {
    x.style.WebkitAnimation = "mymove 4s 2"; // Code for Chrome, Safari and Opera
    x.style.animation = "mymove 4s 2";       // Standard syntax
}

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

// Standard syntax
x.addEventListener("animationstart", myStartFunction);
x.addEventListener("animationiteration", myRepeatFunction);
x.addEventListener("animationend", myEndFunction);

function myStartFunction() {
    this.innerHTML = "animationstart event occured - The animation has started";
    this.style.backgroundColor = "pink";
}

function myRepeatFunction() {
    this.innerHTML = "animationiteration event occured - The animation was played again";
    this.style.backgroundColor = "lightblue";
}

function myEndFunction() {
    this.innerHTML = "animationend event occured - The animation has completed";
    this.style.backgroundColor = "lightgray";
}
</script>

</body>
</html>

Related Tutorials