Here's one approach. To get the ball to bounce X times, we can create a recursive function which takes advantage of jQuery animation queues:
function bounce(n) {
if (n > 0) {
$("#ball").animate({top: "-=5px"}, 150)
.animate({top: "+=5px"}, 150, function() {
bounce(n-1);
});
}
};
bounce(3);
To get it to roll afterwards and continue bouncing, we need to use .dequeue()
to run both animations at once:
$("#ball").animate({"left": "850px"}, 2800).dequeue();
bounce(10);
Now, to combine them, create a special callback function which will only run after the Xth bounce:
function bounce(n, callback) {
if (n > 0) {
$("#ball").animate({top: "-=5px"}, 150)
.animate({top: "+=5px"}, 150, function () {
bounce(n-1, callback);
});
} else if (callback) { // might be undefined
callback();
}
};
bounce(3, function() {
$("#ball").animate({"left": "850px"}, 2800).dequeue();
bounce(10);
});
http://jsfiddle.net/mblase75/c72Qj/