A non-nested animation sequence in jQuery?

You can make a custom .queue() to avoid the limitless nesting..

var q = $({});

function animToQueue(theQueue, selector, animationprops) {
    theQueue.queue(function(next) {
        $(selector).animate(animationprops, next);
    });
}

// usage
animToQueue(q, '#first', {width: '+=100'});
animToQueue(q, '#second', {height: '+=100'});
animToQueue(q, '#second', {width: '-=50'});
animToQueue(q, '#first', {height: '-=50'});

Demo at http://jsfiddle.net/gaby/qDbRm/2/


If, on the other hand, you want to perform the same animation for a multitude of elements one after the other then you can use their index to .delay() each element’s animation for the duration of all the previous ones..

$('li.some').each(function(idx){
    var duration = 500; 
    $(this).delay(duration*idx).animate({ width: '+=100' }, duration);
});

Demo at http://jsfiddle.net/gaby/qDbRm/3/

Leave a Comment