I'm trying to animate elements on a page so that they appear once the user scrolls down to the element. Each element that needs to be animated is marked with class="hideme". And the following function is used:
$(document).ready(function() {
$(window).scroll( function(){
$('.hideme').each( function(i){
var bottom_of_object = $(this).position().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
if( bottom_of_window > bottom_of_object ){
$(this).eq(i).animate({'opacity':'1'},2000);
}
});
});
However, this animated all elements at once instead of just when the user scrolls to that element.
I have read suggestions about using delay functions, but that won't work, because we want to load the element only when the user actually scrolls to that element. I've also tried using eq(), but I don't think that's working either.
Question: How do I animate each element only when the user scrolls to it?
JSFiddle: http://jsfiddle.net/6mochuff/