Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Created August 25, 2020 11:59
Show Gist options
  • Save NetanelBasal/5f344e1ef710d941a3340ef56fb61847 to your computer and use it in GitHub Desktop.
Save NetanelBasal/5f344e1ef710d941a3340ef56fb61847 to your computer and use it in GitHub Desktop.
import { Directive, ElementRef } from '@angular/core';
@Directive({ selector: 'img' })
export class LazyImgDirective {
constructor({ nativeElement }: ElementRef<HTMLImageElement>) {
const supports = 'loading' in HTMLImageElement.prototype;
if (supports) {
nativeElement.setAttribute('loading', 'lazy');
} else {
// fallback to IntersectionObserver
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment