Skip to content

Instantly share code, notes, and snippets.

@devongovett
Created July 29, 2014 20:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devongovett/e2d927cf70e294071479 to your computer and use it in GitHub Desktop.
Save devongovett/e2d927cf70e294071479 to your computer and use it in GitHub Desktop.
var $ = require('jquery');
var transitionEvents = 'transitionend msTransitionEnd webkitTransitionEnd';
var visibilityEvents = 'visibilitychange mozvisibilitychange msvisibilitychange webkitvisibilitychange';
/**
* Runs the given callback when the next transition on the selected element finishes.
* Sometimes the transitionend event doesn't fire when the page is in a
* background tab, so we also handle the page visibility events here too.
*/
$.fn.transitionEnd = function(callback) {
var self = this;
var doc = $(document);
self.on(transitionEvents, transitionEnd);
doc.on(visibilityEvents, transitionEnd);
function transitionEnd() {
self.off(transitionEvents, transitionEnd);
doc.off(visibilityEvents, transitionEnd);
callback.call(self);
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment