0

Possible Duplicate:
JQuery .each() backwards

I am using this code to show each div -> this with a delay an an animation:

$.fn.slideDelay = function(){
    var delay = 0;
    return this.each(function(){
        $(this).delay(delay).animate({
            opacity: 1,
            top: 0
        }, 2000);
        delay += 1000;
    });
};

Works well so far... the only Problem i got is that i need to start with the last index not the first one. Which means the last div gets animated first then the beforelast and so on until the first one.

Community
  • 1
  • 1
ggzone
  • 3,661
  • 8
  • 36
  • 59

4 Answers4

1

Theoretically the following should work:

$.fn.slideDelay = function() {
    var len = this.length,
        delay = (len - 1) * 1000;

    return this.each(function() {
        $(this).delay(delay).animate({
            opacity: 1,
            top: 0
        }, 2000);
        delay -= 1000;
    });
};

DEMO: http://jsfiddle.net/q4cGP/

VisioN
  • 143,310
  • 32
  • 282
  • 281
  • To reverse the delays of three elements (`0 - 1000 - 2000`), I think you'd have to start with `(len - 1) * 1000`. Currently, in case of 3 elements you start with 3000. – pimvdb Oct 12 '12 at 10:04
  • @pimvdb Yes, you're right. I forgot about the indexing. Now it should be fine. Thanks! :) – VisioN Oct 12 '12 at 10:08
1

You could reverse the collection...

collection = Array.prototype.reverse.call(collection);

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
1

Just retrieve the array from your jQuery object and reverse it:

var items = jQuery(this).get();
items.reverse();
jQuery.each(items, function() {.....
stlvc
  • 833
  • 5
  • 16
0

Map the delay with a function like this:

$.fn.slideDelay = function(){
    var max = this.length - 1;
    return this.each(function(i){
        $(this).delay((max - i) * 1000).animate({
            opacity: 1,
            top: 0
        }, 2000);
    });
};
pimvdb
  • 151,816
  • 78
  • 307
  • 352