Skip to content

Instantly share code, notes, and snippets.

@realtomaszkula
Last active June 3, 2018 19:59
Show Gist options
  • Save realtomaszkula/4ae80112061b925a2fdbfcb4725cb3de to your computer and use it in GitHub Desktop.
Save realtomaszkula/4ae80112061b925a2fdbfcb4725cb3de to your computer and use it in GitHub Desktop.
@Directive({
selector: 'img[appLazyLoad]'
})
export class LazyLoadDirective implements AfterViewInit {
@HostBinding('attr.src') srcAttr = null;
@Input() src: string;
ngAfterViewInit() {
this.canLazyLoad() ? this.lazyLoadImage() : this.loadImage();
}
private canLazyLoad() {
return window && 'IntersectionObserver' in window;
}
private lazyLoadImage() { }
private loadImage() {
this.srcAttr = this.src;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment