Skip to content

Instantly share code, notes, and snippets.

@Rob-ot
Last active October 2, 2015 17:38
Show Gist options
  • Select an option

  • Save Rob-ot/2287067 to your computer and use it in GitHub Desktop.

Select an option

Save Rob-ot/2287067 to your computer and use it in GitHub Desktop.
;(function () {
var Modernizr
// run cb when the next animation is complate (or right away if there is no animation support)
var cssTransitionEnd = function (cb) {
var $self = $(this)
var transEndEventNames = {
'WebkitTransition' : 'webkitTransitionEnd',
'MozTransition' : 'transitionend',
'OTransition' : 'oTransitionEnd',
'msTransition' : 'msTransitionEnd', // maybe?
'transition' : 'transitionEnd'
}
var transEndEventName = transEndEventNames[Modernizr.prefixed("transition")]
function unbind () {
$self.off(transEndEventName, animEnd)
}
// animations propogate so only cb if it was fired on this elem
function animEnd (e) {
if (e.target == e.currentTarget) {
if (cb(e.originalEvent.propertyName) !== false) unbind()
}
}
if (Modernizr.csstransitions) {
$self.on(transEndEventName, animEnd)
}
else {
// just run the cb now because the event will never be fired
setTimeout(function () {
cb("")
}, 0)
}
return {
unbind: unbind
}
}
if (typeof define !== 'undefined' && define.amd) {
// AMD
define(['jquery', 'modernizr'], function ($, ModernizrDep) {
Modernizr = ModernizrDep
$.fn.cssTransitionEnd = cssTransitionEnd
return $
})
}
else {
$.fn.cssTransitionEnd = cssTransitionEnd // old skool browser
}
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment