Skip to content

Instantly share code, notes, and snippets.

@nabrown
Last active November 30, 2018 16:57
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 nabrown/a31c9bc21f062b38213ba1e56b0d8472 to your computer and use it in GitHub Desktop.
Save nabrown/a31c9bc21f062b38213ba1e56b0d8472 to your computer and use it in GitHub Desktop.
Unveil() function from jQuery.unveil.js
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