Skip to content

Instantly share code, notes, and snippets.

@bob-lee
Created November 24, 2017 18:54
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 bob-lee/9a75fd26feed48b2237d05240e2ed13f to your computer and use it in GitHub Desktop.
Save bob-lee/9a75fd26feed48b2237d05240e2ed13f to your computer and use it in GitHub Desktop.
IntersectionObserver setup 2
intersectionObserver = new IntersectionObserver(function(entries) {
const entry = entries[0]; // observe one element
const currentRatio = intersectionRatio;
const newRatio = entry.intersectionRatio;
const boundingClientRect = entry.boundingClientRect;
const scrollingDown = currentRatio !== undefined && newRatio < currentRatio &&
boundingClientRect.bottom < boundingClientRect.height;
intersectionRatio = newRatio;
if (scrollingDown) {
// it's scrolling down and observed image started to hide.
// so unobserve it and start loading next images.
const i = indexToObserve + INTERSECT_PAGESIZE;
unobserve();
indexToObserve = i; // set next index and load two more images
console.info(currentRatio + ' -> ' + newRatio + ' [' + i + ']');
}
}, { threshold: [0, 0.25, 0.5, 0.75, 1] });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment