Created
February 9, 2015 16:44
-
-
Save kostasx/2571ddfb3b03dc2c3dae to your computer and use it in GitHub Desktop.
Cross Browser CSS3 TransitionEnd Event Handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Cross Browser CSS3 Transition Event Handler | |
* Source: http://stackoverflow.com/questions/5023514/how-do-i-normalize-css3-transition-functions-across-browsers | |
* @return String : TransitionEnd Event Name | |
*/ | |
function transitionEndEventName () { | |
var i; | |
var el = document.createElement('div'); | |
var transitions = { | |
'transition':'transitionend', | |
'OTransition':'otransitionend', // oTransitionEnd in very old Opera | |
'MozTransition':'transitionend', | |
'WebkitTransition':'webkitTransitionEnd' | |
}; | |
for (i in transitions) { | |
if (transitions.hasOwnProperty(i) && el.style[i] !== undefined) { | |
return transitions[i]; | |
} | |
} | |
//TODO: throw 'TransitionEnd event is not supported in this browser'; | |
} | |
// EXAMPLE USAGE: Check if 'top' CSS property's transition has ended and remove listener | |
var nodeElement = document.querySelector(".container"); | |
function theFunctionToInvoke(e){ | |
if ( e.propertyName === "top" ){ | |
nodeElement.removeEventListener( transitionEnd, theFunctionToInvoke, false); | |
alert(); | |
// DO OTHER STUFF HERE... | |
} | |
} | |
var transitionEnd = transitionEndEventName(); | |
nodeElement.addEventListener( transitionEnd, theFunctionToInvoke, false); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment