Created
November 24, 2017 18:54
-
-
Save bob-lee/9a75fd26feed48b2237d05240e2ed13f to your computer and use it in GitHub Desktop.
IntersectionObserver setup 2
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
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