Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Created May 9, 2012 04:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ScottPhillips/2641904 to your computer and use it in GitHub Desktop.
Save ScottPhillips/2641904 to your computer and use it in GitHub Desktop.
Fade in on Scroll
$(document).ready(function() {
/* Hide all elements outside the visible window */
$('body *').each( function(){
var top_of_object = $(this).position().top;
var bottom_of_window = $(window).scrollTop() + $(window).height();
if( bottom_of_window < top_of_object ){
$(this).addClass('hideme').css({'opacity':'0'});
}
});
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of the desired elements */
$('.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 + 20 ) ){
$(this).removeClass('hideme').animate({'opacity':'1'},500);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment