Javascript DOM transitionend Event

Introduction

Do something with a <div> element when a CSS transition has ended.

Hover over the div element below, and wait for the transition effect to end.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {/* w w  w  .  j a  v a2s . com*/
  width: 100px;
  height: 100px;
  background: red;
  -webkit-transition: width 2s; /* For Safari 3.1 to 6.0 */
  transition: width 2s;
}

#myDIV:hover {
  width: 400px;
}
</style>
</head>
<body>
<div id="myDIV"></div>

<script>
// Code for Safari 3.1 to 6.0
document.getElementById("myDIV").addEventListener("webkitTransitionEnd", myFunction);

// Standard syntax
document.getElementById("myDIV").addEventListener("transitionend", myFunction);

function myFunction() {
  this.innerHTML = "transitionend event occurred - The transition has completed";
  this.style.backgroundColor = "pink";
}
</script>

</body>
</html>

The transitionend event occurs when a CSS transition has completed.

If the transition is removed before completion, the transitionend event will not fire.

Bubbles: Yes
Cancelable: Yes
Event type: TransitionEvent
DOM Version: Level 3 Events



PreviousNext

Related