Sequentially chain your callbacks in jQuery – Three ways
I had a challenge to get four divs to fade in sequentially.
Writing this out the long way is not really my favorite:
$("#vehicle1").fadeIn("slow", function(){ $("#vehicle2").fadeIn("slow", function(){ $("#vehicle3").fadeIn("slow", function(){ $("#vehicle4").fadeIn("slow"); }); }); });
Ewwww. right?
After some conversation in the #jQuery IRC channel, I present twothree classier ways of accomplishing the same thing:
Self-executing callback chaining
(function showVehicle(elem){ elem.fadeIn('slow',function(){ $(this).next().length && showVehicle($(this).next()); }); })( $("div.vehicle:first") );
Custom event triggering (via: ajpiano)
$('div.vehicle') .bind('showVehicle',function(e) {$(this).fadeIn('slow',function(){ $(this).next().length && $(this).next().trigger("showVehicle"); })}) .eq(0) .trigger('showVehicle');
Self-executing callback chain on an arbitrary jQuery object (via: temp01)
(function hidenext(jq){ jq.eq(0).fadeOut("fast", function(){ (jq=jq.slice(1)).length && hidenext(jq); }); })($('div#bodyContent a'))
I won't have you diss my way of doing it on IRC ;)
Here it another way of doing a recursive call with jQuery:
It might not be all that pretty and jQuery-like, but it still works.. :)
Another way – works on any jquery object(not just on sibling elements):
@temp01 That is very very nice, temp01. As far as flexibility is concerned, I think that's the nicest implementation here.
@Paul Irish
I'm trying to develop a similar effect of the ted.com site where elements load sequentially over time. Can this solution work for that with times (500, 1000, 1200) instead of range settings (slow, medium, fast)?
@Trey Yah totally. Just replace the 'fast' shorthand with a millisecond value.
You could also do something like:
for some randomness.
i have just achieved the effect by a simple computer science 101 recursive function:
another prettier way to do it, rather than waiting for the callback to fire, is to start each element fading up at a certain increment, say 200ms apart each, but have each element take longer than the triggering increment to complete its fade up. this is a prettier look.
@temp01
really neat huh?
Here is one that I've been using. It fades in items sequentially then after a brief pause slowly fades them out again.
This one comes from NetTuts (http://tinyurl.com/ycm4bap), slightly modified to use a named function instead of arguments.callee (depricated):
@temp01 Cool! Favorite script of the day
@temp01
Is this executable code? (A function in brackets, etc.?) I would love a short explanation how this works.
@carl
Yes, it's executable. The '(function foo(){ })(…)' is just a self-invoking function, see Decoding Self-Invoking Anonymous Functions.
I've created a simple plugin that can fade in elements sequentially.
http://www.thewebsqueeze.com/web-design-tutorials/sequential-fade-in-jquery-plug-in.html
While the code is not nearly as elegant as the ones listed here, the function call is nice, simple, readable, and chain-able.
An example function call with my plugin is:
$(elem).children().fadeInSequence();
great code! temp01!!
I have question though.. how to invoke the function again once it finished?
Thank you..!
hi temp01;
im new to this , is there a way to post a demo ? ( like displaying result from remote file )
tks
jeff
@j
http://jsfiddle.net/AEPEP/ check the console
I am using Temp01's method and was wondering how it would be possible to reverse the sequence to start from the last item or to even start from any item and fade in the remaining items.
Thanks
@temp01
Thanks for this awesome snippet, it works well and is helpful. I am having a hard time understanding how to run more than the fadeOut operation with this code, i would like to make a pulsing effect by fadein in then fading out for each item. Appreciate any response
@August
I figured it out lol…. heres an example for my situation in which i wanted a pulsing effect for each item in the selector at the end, hopefully it helps
(function hidenext(jq){
jq.eq(0).fadeOut(100, function(){
$(this).fadeIn(100, function() {
});
(jq=jq.slice(1)).length && hidenext(jq);
});
})($('.homeherowrap > div'))
I don't like jQuery, but for me
Is the easiest to read…