Last active
November 30, 2018 16:57
-
-
Save nabrown/a31c9bc21f062b38213ba1e56b0d8472 to your computer and use it in GitHub Desktop.
Unveil() function from jQuery.unveil.js
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 unveil() { | |
// create an array of images that are in view | |
// by filtering the intial array | |
var inview = images.filter(function() { | |
var $img = $(this); | |
// if the image is hidden, don't bother | |
if ($img.is(":hidden")) return; | |
var wt = $w.scrollTop(), // window vertical scroll distance | |
wb = wt + $w.height(), // last point of document visible in browser window | |
et = $img.offset().top, // distance from document top to top of element | |
eb = et + $img.height(); // distance from top of document to bottom of element | |
// the bottom of the element is below the top of the browser (- threshold) | |
// && the top of the element is above the bottom of the browser (+ threshold) | |
return eb >= wt - th && et <= wb + th; | |
}); | |
// trigger an 'unveil' event on each of the inview images | |
inview.trigger("unveil"); | |
// filter the images to only those that aren't in view yet | |
images = images.not(inview); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment