Lazy loading

Angular Bootstrap 5 Lazy loading

Bootstrap 5 Lazy Loading is a feature, that allows you to load images or videos only when they are visible on the screen.

Note: Read the API tab to find all available options and advanced customization


Basic example

Lazy Loading will automatically initialize after adding mdbLazyLoad directive to your img or video element. It is important to add [attr.data-src] attribute - otherwise, directive will throw an error. You can use [container] input to bind directive to specific parent container. In this case, the direcitve will listen to the container scroll event.

Scroll down to see an image

Example image

Offset

Use [offset] input to define an additional amount of pixels after which image or video will show.

Scroll more down to load a picture.

Example image

Error

Use [errorPlaceholder] input to define a picture that will show if image or video doesn't load.

Example image

Animations

Example image
Example image

Lazy loading - API


Import


        import { MdbLazyLoadingModule } from 'mdb-angular-ui-kit/lazy-loading';
        …
        @NgModule ({
          ...
          imports: [MdbLazyLoadingModule],
          ...
        })
      

Inputs

Name Type Default Description
container HTMLElement - Defines parent container of the element
delay number 0 Defines delay after which element will show
errorPlaceholder string - Defines a picture that is shown if an error with showing element occurs
offset number 0 Defines an additional offset after which element will show
loadingPlaceholder string - Defines a picture that is shown before loading a proper element

Outputs

Name Type Description
loadingEnd EventEmitter<void> Event fired when element is fully loaded
loadingError EventEmitter<void> Event fired if error occurs while loading
loadingStart EventEmitter<void> Event fired when loading starts

            <img
              mdbLazyLoading
              (loadingStart)="onLoadingStart"
              [loadingPlaceholder]="'https://place-hold.it/838x471?text=Loading'"
              [attr.data-src]="'https://mdbootstrap.com/img/Photos/Slides/img%20(102).jpg'"
              alt="Example image"
              class="img-fluid my-3"
            />
          

          import { Component } from '@angular/core';

          @Component({ selector: 'app-lazy-loading',
            templateUrl: './lazy-loading.component.html',
            styleUrls: ['./lazy-loading.component.scss'],
          })
          export class LazyLoadingComponent {
            onLoadingStart() {
              console.log('loading start');
            }

            constructor() {}
          }