views:

46

answers:

4

Hello,

I have created a jquery animation that can be seen by clicking the Preview button on top:

http://jsbin.com/anayi3/edit

I have used the slideDown animation method. The problem is that all items slide down together. I want to each number to display after short delay. It should not be much of problem for experienced jquery developers.

How to do that?

Thanks in advance.

+1  A: 

You'l need to use the callback function to animate the next number after the first has finished. See the documentation.

Something like:

$('firstNumber').slideDown('slow',function(){ 
    $('secondNumber').slideDown('slow');
});

You'll need to call this recursively for each of the numbers.

Fermin
Could you please modify the script so that it shows items after some delay. You can edit it there http://jsbin.com/anayi3/edit. Thanks
Sarfraz
+1  A: 

Try this:

var index = 0;

function animate() {
  elem = $("div:eq(" + index + ")");
  elem.slideDown('slow');
  index++;
  if (index < 10) {
    setTimeout(animate, 500);
  }
}

$(function(){

  for(var i = 1; i < 10 ;i++){
    var elem = $('<div>').html('Number : ' + i).appendTo($('body')).hide();
  }
  animate();
});
kgiannakakis
+1  A: 

Basically you need to attach all animations to some element's queue in order to run them sequentially

$(function(){
    $.each([1, 2, 3, 4, 5, 6, 7, 8, 9], function() {
        var elem = $('<div>').html('Number : ' + this).appendTo($('body')).hide();
        $("body").queue(function(next) {
            elem.slideDown('slow', next)
        });
    });
});

Here all animations are attached to "body", but that can be any element. The "next" syntax is new in jquery 1.4.

stereofrog
+3  A: 

An example using recursive anonymous function. Put this inside your document-ready function.

  // Array of elements to be animated.
  var elements = $('.your-selector-here li').get();

  (function () {
    // Take out the first element of array, animate it
    // and pass this anonymous function as callback.
    $(elements.shift()).slideDown('slow', arguments.callee);
  })();
​
jholster
This is totally smart and elegant. Welcome to SO, Yaggo!
stereofrog
@Yaggo: great: never thought it can be done that way. Thanks :)
Sarfraz
Thanks for your warm welcome, stereofrog!
jholster