Created
March 12, 2010 20:59
-
-
Save cowboy/330793 to your computer and use it in GitHub Desktop.
jQuery queueFn: add a $.fn method or function onto the effects queue
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
/*! | |
* jQuery queueFn - v0.5 - 06/18/2010 | |
* http://benalman.com/ | |
* | |
* Copyright (c) 2010 "Cowboy" Ben Alman | |
* Dual licensed under the MIT and GPL licenses. | |
* http://benalman.com/about/license/ | |
*/ | |
(function($){ | |
'$:nomunge'; // Used by YUI compressor. | |
var defaults = { | |
queue: 'fx' | |
}; | |
$.fn.queueFn = function() { | |
var args = Array.prototype.slice.call( arguments ), | |
options = $.extend( {}, defaults, typeof args[0] === 'object' && args.shift() ), | |
fn = args.shift(); | |
fn = $.isFunction( fn ) ? fn : $.fn[ fn ]; | |
return this.queue( options.queue, function(next){ | |
fn.apply( $(this), args ); | |
next(); | |
}); | |
}; | |
})(jQuery); | |
// usage: | |
// | |
// $('#foo').fadeOut().queueFn( 'remove' ); | |
// | |
// $('#bar').hide().addClass( 'fading' ).fadeIn().queueFn( 'removeClass', 'fading' ); | |
// | |
// function remove( state ){ state && $(this).remove(); }; | |
// $('#baz').fadeOut().queueFn( remove, some_condition ); |
This gist has now been "pluginized" and is available here: jQuery queueFn.
Note that v0.5 shown here is overkill. Since nobody using this plugin is going to want to use a custom animation queue, v0.4 is what's currently "live" as the official plugin.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much! This is exactly what I wanted. I believe that this code should be included in jquery's core as it is rather useful.