Created
October 17, 2013 18:09
-
-
Save tancredi/7029550 to your computer and use it in GitHub Desktop.
Function to attach a callback to the end of the current/next CSS animation. Useful when dealing with chained animations.
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
function getPrefixedEvents (str) { | |
var prefixes = [ '', 'webkit', 'o', 'MS', 'moz' ], | |
out = [], | |
i, prefix, cur; | |
for (i = 0; i < prefixes.length; i += 1) { | |
prefix = prefixes[i]; | |
cur = prefix + str; | |
if (prefix.length < 2) { | |
cur = cur.toLowerCase(); | |
} | |
out.push(cur); | |
} | |
return out; | |
} | |
function onceAnimationFinished (element, callback) { | |
var events = getPrefixedEvents('AnimationEnd'); | |
function getListener (i) { | |
return function listener () { | |
console.log() | |
element.removeEventListener(events[i], listener, false); | |
callback(); | |
}; | |
} | |
for (var i = 0; i < events.length; i += 1) { | |
element.addEventListener(events[i], getListener(i), false); | |
} | |
} |
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 el = document.getElementById('foo'); | |
onceAnimationFinished(el, function () { | |
// [..] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment