Last active
August 29, 2015 14:15
-
-
Save jkusachi/6e226278ab52628146dd to your computer and use it in GitHub Desktop.
Javascript Image Fade In / Fade Out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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