/ Gists / Multiple animation with one finish callback
On gists

Multiple animation with one finish callback

jQuery
@live: http://jsfiddle.net/c6UEm/1/

demo.js Raw #

$(document).ready(function () {
    animate([$('#one'), $('#two'), $('#three')], finished);
});

function finished() {
    $('body').append('Finished');
}

function animate(list, callback) {
    if (list.length === 0) {
        callback();
        return;
    }
    $el = list.shift();
    $el.animate({left: '+=200'}, 1000, function () {
        animate(list, callback);
    });
}

demo.css Raw #

container {
    position: relative;
}

.box {
    position: absolute;
    width: 50px;
    height: 50px;
    left: 100px;
    top: 40px;
}

#one { background-color: #F33; }
#two { background-color: #F99; }
#three { background-color: #9F9; }

demo.html Raw #

<body>
  <div id="container">
    <div id="one" class="box"></div>
    <div id="two" class="box"></div>
    <div id="three" class="box"></div>
  </div>
</body>