Skip to content

Instantly share code, notes, and snippets.

@tbusser
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbusser/3f91394e4996cb8e4161 to your computer and use it in GitHub Desktop.
Save tbusser/3f91394e4996cb8e4161 to your computer and use it in GitHub Desktop.
Determine the name of the `transtitionend` event
var NO_TRANSITIONS = 'no-transitions';
/**
* This method tries to determine the name of the transitionend event, it
* could be the user's browser is using a prefixed version
*/
function getTransitionEndEventName () {
// 1: The variable we use to keep track of the current key when
// iterating over the transitions object
// 2: An element we can use to test if a CSS property if known to the
// browser
// 3: The key:value pairs represent CSS-property:Event-name values. By
// testing if the browser supports a CSS property we can tell what
// name to use for the transitionend event
// 4: oTransitionEnd in very old Opera
var key, /* [1] */
el = document.createElement('div'), /* [2] */
transitions = { /* [3] */
WebkitTransition : 'webkitTransitionEnd',
OTransition : 'otransitionend', /* [4] */
MozTransition : 'transitionend',
transition : 'transitionend'
};
// Loop over the keys in the transition object
for (key in transitions) {
// Check the key is a property of the object and check if the CSS
// property does not return undefined. If the result is undefined it
// means the browser doesn't recognize the (un)prefixed property
if (transitions.hasOwnProperty(key) && el.style[key] !== undefined) {
// The CSS property exists, this means we know which name of the
// transitionend event we can use for this browser
return transitions[key];
}
}
// If the method reaches this line it means that none of the CSS
// properties were recognized by the browser. It is safe to conclude
// there is no support for transitions (or at least none that we can use
// in any meaningful way)
return NO_TRANSITIONS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment