ngx-lazy-load-images

Image lazy load library for Angular 2+.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
ngx-lazy-load-images
80182.0.02 years ago5 years agoMinified + gzip package size for ngx-lazy-load-images in KB

Readme

ngx-lazy-load-images
FOSSA Status
ngx-lazy-load-images is a image lazy load library for Angular 2+.
The library allows to lazy load images from your web application using the MutationObserver and the IntersectionObserver. Images will be loaded as soon as they enter the viewport in a non-blocking way.
It supports <img> tags as well as background images.

Installation

You can install the library via npm with this command:
npm install ngx-lazy-load-images --save

Usage

  1. Import the LazyLoadImagesModule

Import LazyLoadImagesModule from the ngx-lazy-load-images package, and add it to the NgModule imports array of your component.
import { NgModule } from '@angular/core';
import { LazyLoadImagesModule } from 'ngx-lazy-load-images';

@NgModule({
  imports: [
    LazyLoadImagesModule
  ]
})
export class AppComponent {}

  1. Use the lazy-load-images directive

Add the lazy-load-images directive to the tag containing all the DOM image nodes to lazy load:
<!-- Image tags -->
<div class="image-list" lazy-load-images>
  <img *ngFor="let imageUrl in images" [attr.data-src]="imageUrl">
</div>

<!-- Background images -->
<div class="image-list" lazy-load-images>
  <div *ngFor="let imageUrl in images" [attr.data-background-src]="imageUrl"></div>
</div>

The container can have any HTML or components inside along with the images. But the less nodes inside the directive, the faster it will be.
Intersection Observer Configuration
If you want to make some configuration changes to the Intersection Observer, you can do it by passing an object to the lazy-load-images directive. The object takes the Intersection Observer configuration parameters as keys.
```html
lazy-load-images="{ rootMargin: '50px' }">
```

  1. Set the data-src or data-background-src attribute

Set the data-src or data-background-src attribute in the images or tags that you want to lazy load.
The data-src attribute allows to lazy load images inside <img> tags. You can define it by passing a variable to the attribute, interpolating a variable, or inserting the URL directly:
<img [attr.data-src]="imageUrlVariable">
<img attr.data-src="{{ imageUrlVariable }}">
<img data-src="https://example.com/cute_kitten.jpg">

The data-background-src attribute lazy loads background images inside any HTML tag. It works the same way as the data-src attribute:
<div [attr.data-background-src]="imageUrlVariable"></div>
<div attr.data-background-src="{{ imageUrlVariable }}"></div>
<div data-background-src="https://example.com/cute_kitten.jpg"></div>

  1. Have fun 🎉

Performance

  • Will this module make my web application worse in terms of performance?
- This module will make your app better in terms of overall and render performance, especially if it has a lot of images to show when the page is loaded. The images will be downloaded as soon as they appear in the viewport instead of all together. That means that your users will save network transfer data and the scroll will be less laggy.
  • What happens if I have lots of images, will it consume too much memory?
- The lifespan of the image observers is too short. It will only last until the URL is set to the proper attribute, and destroyed right after that. And if the directive is destroyed, all the remaining image observers and the intersection observer instance will be disconnected.

Browser Compatibility

This library has been tested and known to work in the latest version of all major browsers.
The WICG Intersection Observer Polyfill is bundled within the module to use the Intersection Observer API in the non-compatible browsers. Avoid importing it in your application too, as it may cause unexpected issues.
However, the compatibility with older browsers is limited by the Mutation Observer support, which is known to work in:
<td align="center">
  <img src="https://raw.github.com/alrra/browser-logos/39.2.2/src/chrome/chrome_48x48.png" alt="Chrome"><br>
  26+
</td>
<td align="center">
  <img src="https://raw.github.com/alrra/browser-logos/39.2.2/src/firefox/firefox_48x48.png" alt="Firefox"><br>
  14+
</td>
<td align="center">
  <img src="https://raw.github.com/alrra/browser-logos/39.2.2/src/safari/safari_48x48.png" alt="Safari"><br>
  6+
</td>
<td align="center">
  <img src="https://raw.github.com/alrra/browser-logos/39.2.2/src/edge/edge_48x48.png" alt="Edge"><br>
  ✔
</td>
<td align="center">
  <img src="https://raw.github.com/alrra/browser-logos/39.2.2/src/archive/internet-explorer_7-8/internet-explorer_7-8_48x48.png" alt="Internet Explorer"><br>
11+
</td>
<td align="center">
  <img src="https://raw.github.com/alrra/browser-logos/39.2.2/src/opera/opera_48x48.png" alt="Opera"><br>
  15+
</td>
<td align="center">
  <img src="https://raw.github.com/alrra/browser-logos/39.2.2/src/android/android_48x48.png" alt="Android"><br>
  4.4+
</td>

License

MIT
FOSSA Status