Created
August 6, 2014 21:50
-
-
Save miguel-perez/ca88d24c9872247bafed to your computer and use it in GitHub Desktop.
StackOverflow
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
var | |
/** | |
* Fires a custom event when all animations are complete | |
* @param {object} $element - jQuery object that should trigger event | |
* | |
*/ | |
triggerAllAnimationEndEvent = function ($element) { | |
var animationCount = 0, | |
animationstart = "animationstart webkitAnimationStart oanimationstart MSAnimationStart", | |
animationend = "animationend webkitAnimationEnd oanimationend MSAnimationEnd", | |
eventname = "ss.allanimationend", | |
unbindHandlers = function(e){ | |
$element.trigger(eventname); | |
// utility.redraw($element); | |
console.log(eventname); | |
}, | |
onAnimationStart = function (e) { | |
if ($(e.target).is($element)) { | |
e.stopPropagation(); | |
animationCount ++; | |
} | |
}, | |
onAnimationEnd = function (e) { | |
if ($(e.target).is($element)) { | |
e.stopPropagation(); | |
animationCount --; | |
if(animationCount === 0) { | |
unbindHandlers(); | |
} | |
} | |
}; | |
$element.on(animationstart, onAnimationStart); | |
$element.on(animationend, onAnimationEnd); | |
}, | |
/** | |
* Fires a custom callback when all animations are finished | |
* @param {object} $element - jQuery object that should trigger event | |
* @param {function} callback - function to run | |
* | |
*/ | |
triggerCallback= function ($element, callback) { | |
$element.one("ss.allanimationend", callback); | |
// Fires fake animation events in case no animations are used | |
setTimeout(function(){ | |
$element.trigger("animationstart"); | |
$element.trigger("animationend"); | |
}, 100); // wait a bit | |
}; | |
triggerAllAnimationEndEvent($('.element')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment