I am fading elements one by one but it seems it all fades at once.
How can I fade elements one by one. Only if one fades completely, should the second start fading.
I loop and fade it like this
$(ele).fadeIn('slow');
I am fading elements one by one but it seems it all fades at once.
How can I fade elements one by one. Only if one fades completely, should the second start fading.
I loop and fade it like this
$(ele).fadeIn('slow');
fadeIn has a callback that executes when fading is completed. Add to every element the class elemX, where x is the order of fading. Then use the following code:
startFading(1);
function startFading(order) {
$(".ele" + order).fadeIn('slow', function() {
if (order < orderMax) {
startFading(order+1);
}
});
}
I made this quick/easy jQuery plugin for you to do just what you want. :-)
$.fn.extend({
serial_fade: function(o) {
if(!o.speed || o.speed == undefined || o.speed == null) { o.speed = 'slow'; }
if(!o.fade || o.fade == undefined || o.fade == null) { o.fade = 'in'; }
if(!o.index || o.index == undefined || o.index == null) { o.index = 0; }
var s = this.selector;
if(o.fade.toLowerCase() == 'in') {
return this.eq(o.index).fadeIn(o.speed, function() {
o.index++;
if($(s).eq(o.index).length > 0) {
$(s).serial_fade({speed:o.speed,fade:o.fade,index:o.index});
}
});
} else {
return this.eq(o.index).fadeOut(o.speed, function() {
o.index++;
if($(s).eq(o.index).length > 0) {
$(s).serial_fade({speed:o.speed,fade:o.fade,index:o.index});
}
});
}
}
});
// To call it just do this:
$(ele).serial_fade({speed:'slow',fade:'in'});
// Optionally, you can pass which element you want to start with (0-based):
$('a').serial_fade({speed:'slow',fade:'in',index:2});
// If you want to start with element 2 (3, really) and fade all the rest *out*
// sequentially, verrry slowly:
$(ele).serial_fade({speed:5000,fade:'out',index:2});
It should work with ANY kind of selector just like any other jQuery method does. I hope this works out for you.
Edit: I extended it so that it can do fade ins and fade outs now. It just seems more useful that way...
You could make this generic and not force it to be for fading only.
function depth(collection, fun, i) {
if (i === undefined)
depth(collection, fun, 0);
else
if (i < collection.length)
fun(collection[i], function(){
depth(collection, fun, i + 1);
});
};
depth($("a"), function(elem, fun) {
$(elem).fadeIn('slow', fun);
});