Skip to content

Instantly share code, notes, and snippets.

@INDIAN2020
Forked from ScottPhillips/fade-in-on-scroll.js
Created May 29, 2014 04:33
Show Gist options
  • Save INDIAN2020/08f6a2bbfbb436db13df to your computer and use it in GitHub Desktop.
Save INDIAN2020/08f6a2bbfbb436db13df to your computer and use it in GitHub Desktop.
$(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