Last active
June 3, 2018 20:03
-
-
Save realtomaszkula/081d0ebab30aa7c5b98c7b583fd7d244 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@Directive({ | |
selector: 'img[appLazyLoad]' | |
}) | |
export class LazyLoadDirective implements AfterViewInit { | |
// ... | |
constructor(private el: ElementRef) {} | |
private lazyLoadImage() { | |
const obs = new IntersectionObserver(entries => { | |
entries.forEach(({ isIntersecting }) => { | |
if (isIntersecting) { | |
this.loadImage(); | |
obs.unobserve(this.el.nativeElement); | |
} | |
}); | |
}); | |
obs.observe(this.el.nativeElement); | |
} | |
private loadImage() { | |
this.srcAttr = this.src; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment