I'm trying to animate (fade in) several headings on a page as the user scrolls to them, but when I reach the first one it fades them all in, regardless of whether I've reached them yet.
Could somebody help me to figure out where I'm going wrong? The jQuery looks like:
$(window).scroll( function(){
$('.section').each( function(i){
var bottom_of_object = $(this).position().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
if( $('hgroup').is('.heading1') && bottom_of_window > bottom_of_object ){
$('.heading1').animate({'opacity':'1'},1000);
}
if( $('hgroup').is('.heading2') && bottom_of_window > bottom_of_object ){
$('.heading2').animate({'opacity':'1'},1000);
}
});
});
And the markup:
<section class="section">
<hgroup class="heading1">
<h1>Title</h1>
<h2>Sub-Title</h2>
</hgroup>
</section>
<section class="section">
<hgroup class="heading2">
<h1>Title</h1>
<h2>Sub-Title</h2>
</hgroup>
</section>