Reputation: 23
I'm using this code I found to fadein elements(with class:hideme) when I scroll to it, but it fades in only when it's fully visible, is there a way I can adjust the code so it fades in as soon as it appears in the browser?
I tried to change the ">" to "=" in this line and it kinda works:
if( bottom_of_window > bottom_of_object ){
but it's not working when i apply to the second elements
thanks!
original link: http://jsfiddle.net/tcloninger/e5qaD/
here's the code:
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').each( function(i){
var bottom_of_object = $(this).position().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if( bottom_of_window > bottom_of_object ){
$(this).animate({'opacity':'1'},500);
}
});
});
});
Upvotes: 2
Views: 115
Reputation: 8643
You only need to check for the top of the object
var top_of_object = $(this).position().top;
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the top of the object is visible in the window, fade it in */
if( bottom_of_window >= top_of_object ){
$(this).animate({'opacity':'1'},500);
}
Updated JSFiddle here: http://jsfiddle.net/e5qaD/2727/
Upvotes: 1