New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should use intersection observer when possible #13
Comments
Do something like this: let observer;
// create a single observer for all images
function getObserver(loadImageAndSetLoadedClass, scrollTarget, offset) {
if (observer) return observer;
observer = new IntersectionObserver(changes => {
changes.forEach(change => {
loadImageAndSetLoadedClass(change.target);
observer.unobserve(change.target);
});
}, {
root: scrollTarget,
rootMargin: offset + 'px'
});
return observer;
}
// For each image
getObserver(console.log.bind(console), window, 100).observe(this.elementRef); |
The problem is it's hard to debounce the scroll with the IntersectionObserver , it's not meant to be used like that. So it's more made for sentinel elements https://developers.google.com/web/updates/2016/04/intersectionobserver
|
Yeah, you are right. We have to create a new Thanks for your feedback! |
Listen for scroll events are expensive. Use intersection observer instead.
https://developers.google.com/web/updates/2016/04/intersectionobserver
https://wicg.github.io/IntersectionObserver/
The text was updated successfully, but these errors were encountered: