Created
August 15, 2013 10:34
-
-
Save kapowaz/6239873 to your computer and use it in GitHub Desktop.
A simple convenience method to bind single-use callbacks to the end of transition effects, making chaining transitions a little less callback-hellish.
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
| // convenience method for applying a transition class, with a callback | |
| // usage: | |
| // $('div.foo').transition('zoomed', function(){ console.log('your div is now zoomed!'); }); | |
| // $('div.bar.faded').transition('!faded', function(){ console.log('your div is no longer faded'); }); | |
| jQuery.fn.transition = function(className, callback) { | |
| var element = $(this), | |
| transitionEndEvents = 'webkitTransitionEnd mozTransitionEnd msTransitionEnd oTransitionEnd transitionEnd transitionend', | |
| transitionCallback = function transitionCallback(){ | |
| element.unbind(transitionEndEvents); | |
| if (typeof callback === "function") callback(); | |
| }; | |
| switch (className.indexOf("!")) { | |
| case 0: | |
| element.removeClass(className.substring(1)).on(transitionEndEvents, transitionCallback); | |
| break; | |
| default: | |
| element.addClass(className).on(transitionEndEvents, transitionCallback); | |
| break; | |
| } | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For your next exercise: rewrite this to make use of jQuery.Deferred objects.