Skip to content

Instantly share code, notes, and snippets.

@jkusachi
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkusachi/6e226278ab52628146dd to your computer and use it in GitHub Desktop.
Save jkusachi/6e226278ab52628146dd to your computer and use it in GitHub Desktop.
Javascript Image Fade In / Fade Out
(function($){
'use strict';
function scrolled(){
var scrollTop = $(window).scrollTop(),
windowBottom = scrollTop + $(window).height(),
images_in_view = $('img:in-viewport');
//for each image in view, fade out on bottom
images_in_view.each(function(i){
var
self = $(this),
offset = self.offset().top,
img_height = self.height(),
distance = offset - windowBottom,
visibility = Math.abs(distance) / img_height;
if(visibility > 0.9)
visibility = 1;
if(visibility > 0 && visibility <= 1 )
self.css('opacity', visibility);
});
}
$(window).bind("scroll resize", scrolled);
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment