Created
March 31, 2014 15:04
-
-
Save eirikurn/9894437 to your computer and use it in GitHub Desktop.
Simple helper to css animate element with class
This file contains 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
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