Skip to content

Instantly share code, notes, and snippets.

@eirikurn
Created March 31, 2014 15:04
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 eirikurn/9894437 to your computer and use it in GitHub Desktop.
Save eirikurn/9894437 to your computer and use it in GitHub Desktop.
Simple helper to css animate element with class
var utils = {
/**
* Assigns an animation class to the selected elements, removing it when
* the animation finishes.
* @param {jQuery} el The jQuery element.
* @param {string} name Class name to add.
* @param {Function} cb Callback function when animation finishes.
* @param {Boolean} nowait Call the callback without waiting.
* @param {String} child Child element that runs the animation or transition.
*/
animWithClass: function(el, name, cb, nowait, child) {
var elem = child ? el.find(child) : el;
elem.one(ANIMATION_END + ' ' + TRANSITION_END, function(e) {
el.removeClass(name);
if (cb && nowait) {
cb.apply(el[0]);
} else if (cb) {
setTimeout(function() { cb.apply(el[0]); }, 0);
}
});
el.addClass(name);
}
};
/**
* Figure out cross-browser animation event names.
*/
var ANIMATION_END, TRANSITION_END;
(function() {
var name,
el = document.createElement('div'),
animationNames = {
'WebkitAnimation': 'webkitAnimationEnd',
'MozAnimation': 'animationend',
'OAnimation': 'oAnimationEnd oanimationend',
'animation': 'animationend'
},
transitionNames = {
'WebkitAnimation': 'webkitTransitionEnd',
'MozAnimation': 'transitionend',
'OAnimation': 'oTransitionEnd otransitionend',
'animation': 'transitionend'
};
for (name in animationNames) {
if (el.style[name] !== undefined) {
ANIMATION_END = animationNames[name];
TRANSITION_END = transitionNames[name];
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment