[Angular] Lazy Load Images in Angular

<img src="src.png" alt="Angular" loading="lazy"> 

 

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
    }
  }
}

 

Read More

posted @ 2020-09-01 22:33  Zhentiw  阅读(142)  评论(0编辑  收藏  举报