javascript - CSS3 transition events

ID : 20126

viewed : 30

Tags : javascriptcsseventscss-transitionsjavascript

Top 5 Answer for javascript - CSS3 transition events

vote vote


W3C CSS Transitions Draft

The completion of a CSS Transition generates a corresponding DOM Event. An event is fired for each property that undergoes a transition. This allows a content developer to perform actions that synchronize with the completion of a transition.


To determine when a transition completes, set a JavaScript event listener function for the DOM event that is sent at the end of a transition. The event is an instance of WebKitTransitionEvent, and its type is webkitTransitionEnd.

box.addEventListener( 'webkitTransitionEnd',      function( event ) { alert( "Finished transition!" ); }, false ); 


There is a single event that is fired when transitions complete. In Firefox, the event is transitionend, in Opera, oTransitionEnd, and in WebKit it is webkitTransitionEnd.


There is one type of transition event available. The oTransitionEnd event occurs at the completion of the transition.

Internet Explorer

The transitionend event occurs at the completion of the transition. If the transition is removed before completion, the event will not fire.

Stack Overflow: How do I normalize CSS3 Transition functions across browsers?

vote vote



All modern browsers now support the unprefixed event:

element.addEventListener('transitionend', callback, false);

I was using the approach given by Pete, however I have now started using the following

$(".myClass").one('transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd',  function() {  //do something }); 

Alternatively if you use bootstrap then you can simply do

$(".myClass").one($.support.transition.end, function() {  //do something }); 

This is becuase they include the following in bootstrap.js

+function ($) {   'use strict';    // CSS TRANSITION SUPPORT (Shoutout:   // ============================================================    function transitionEnd() {     var el = document.createElement('bootstrap')      var transEndEventNames = {       'WebkitTransition' : 'webkitTransitionEnd',       'MozTransition'    : 'transitionend',       'OTransition'      : 'oTransitionEnd otransitionend',       'transition'       : 'transitionend'     }      for (var name in transEndEventNames) {       if ([name] !== undefined) {         return { end: transEndEventNames[name] }       }     }      return false // explicit for ie8 (  ._.)   }     $(function () {     $.support.transition = transitionEnd()   })  }(jQuery); 

Note they also include an emulateTransitionEnd function which may be needed to ensure a callback always occurs.

  //   $.fn.emulateTransitionEnd = function (duration) {     var called = false, $el = this     $(this).one($.support.transition.end, function () { called = true })     var callback = function () { if (!called) $($el).trigger($.support.transition.end) }     setTimeout(callback, duration)     return this   } 

Be aware that sometimes this event doesn’t fire, usually in the case when properties don’t change or a paint isn’t triggered. To ensure we always get a callback, let’s set a timeout that’ll trigger the event manually.

vote vote


All modern browsers now support the unprefixed event:

element.addEventListener('transitionend', callback, false);

Works in the latest versions of Chrome, Firefox and Safari. Even IE10+.

vote vote


In Opera 12 when you bind using the plain JavaScript, 'oTransitionEnd' will work:

document.addEventListener("oTransitionEnd", function(){     alert("Transition Ended"); }); 

however if you bind through jQuery, you need to use 'otransitionend'

$(document).bind("otransitionend", function(){     alert("Transition Ended"); }); 

In case you are using Modernizr or bootstrap-transition.js you can simply do a change:

var transEndEventNames = {     'WebkitTransition' : 'webkitTransitionEnd',     'MozTransition'    : 'transitionend',     'OTransition'      : 'oTransitionEnd otransitionend',     'msTransition'     : 'MSTransitionEnd',     'transition'       : 'transitionend' }, transEndEventName = transEndEventNames[ Modernizr.prefixed('transition') ]; 

You can find some info here as well

vote vote


Just for fun, don't do this!

$.fn.transitiondone = function () {   return this.each(function () {     var $this = $(this);     setTimeout(function () {       $this.trigger('transitiondone');     }, (parseFloat($this.css('transitionDelay')) + parseFloat($this.css('transitionDuration'))) * 1000);   }); };   $('div').on('mousedown', function (e) {   $(this).addClass('bounce').transitiondone(); });  $('div').on('transitiondone', function () {   $(this).removeClass('bounce'); }); 

Top 3 video Explaining javascript - CSS3 transition events