This code should work:
var $modules = $('.module');
$modules.each(function(i) {
var $el = $modules.eq(0);
$modules.splice(0, 1);
$el.addClass("fadein").removeClass('module');
});
EXPLAINATION
When you use each
method, jQuery call your callback for each element, passing i
as an index of current element.
So let's say you have an array: [elem1, elem2, elem3, elem4]
.
On the first step i
equals 0. So by writing $el = $modules.eq(i);
you get the first element, as expected. then you remove it form array, and now it looks like that: [elem2, elem3, elem4]
.
On the second step i
equals 1, which means second element of your array, that is elem3 actually.
As you can see you jumped over elem2. Thats why some elements remain in an array.
To avoid this behavior you have to use zero index: var $el = $modules.eq(0);
and $modules.splice(0, 1);
This is a demo