Lazy loading MDB Pro component

Lazy loading - Bootstrap 5 & Material Design 2.0

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 .lazy class to your img or video element. It is important to add data-lazy-src attribute - otherwise, Lazy Load will throw an error.

Scroll down to see an image

Example image

          <div class="container lazy mt-5" style="height: 500px; overflow-y: scroll">
            <div class="row text-center" style="height: 800px">
              <p>Scroll down to see an image</p>

              <p><i class="far fa-arrow-alt-circle-down fa-3x my-4"></i></p>

              <img
                data-lazy-src="https://mdbootstrap.com/img/Photos/Slides/img%20(102).jpg"
                data-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
                alt="Example image"
                class="img-fluid lazy my-3"
                style="margin-top: 50% !important"
              />

              <video
                class="lazy img-fluid"
                data-lazy-src="https://mdbootstrap.com/img/video/Sail-Away.mp4"
                data-lazy-delay="1000"
                data-lazy-placeholder="https://placehold.it/838x471?text=Loading"
                muted="muted"
                autoplay="autoplay"
              ></video>
            </div>
          </div>
        

Offset

Use data-lazy-offset attribute to define an additional amount of pixels after which image or video will show.

Scroll more down to load a picture.

Example image

          <img
            data-lazy-src="https://mdbootstrap.com/img/Photos/Slides/img%20(102).jpg"
            data-lazy-offset="800"
            data-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
            alt="Example image"
            class="img-fluid lazy"
          />
        

Error

Use data-lazy-error to define a picture that will show if image or video doesn't load.

Example image

          <img
            data-lazy-src="sample"
            data-lazy-delay="1000"
            data-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
            data-lazy-error="https://placehold.it/1321x583?text=Error"
            alt="Example image"
            class="img-fluid lazy"
          />
        

Animations

Use data-lazy-animation to specify which animation you want to activate when element loads.

Example image
Example image

          <img
            data-lazy-src="https://mdbootstrap.com/img/Photos/Slides/img%20(102).jpg"
            data-lazy-delay="1000"
            data-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
            data-lazy-animation="zoom-in"
            alt="Example image"
            class="img-fluid lazy"
          />
          <img
            data-lazy-src="https://mdbootstrap.com/img/Photos/Slides/img%20(102).jpg"
            data-lazy-delay="1000"
            data-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
            data-lazy-animation="shake"
            alt="Example image"
            class="img-fluid lazy"
          />
        

Container

Initialize a set of elements at once by adding .lazy class to their container. If you set data-lazy attributes in the parent, all of img and video elements inside will inherit them. You can overwrite them by declaring these attributes in inner elements.

Example image Example image

          <div
            class="container lazy mt-5"
            data-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
            data-lazy-error="https://placehold.it/1321x583?text=Error"
          >
            <img data-lazy-src="." alt="Example image" class="img-fluid mb-3" />

            <img
              data-lazy-src="https://mdbootstrap.com/img/Photos/Slides/img%20(102).jpg"
              alt="Example image"
              class="img-fluid mb-3"
            />

            <video
              class="img-fluid"
              data-lazy-src="https://mdbootstrap.com/img/video/Sail-Away.mp4"
              muted="muted"
              autoplay="autoplay"
            ></video>
          </div>
        

Lazy loading - API


Usage

Via data attributes


        <img
          data-lazy-src="https://mdbootstrap.com/img/Photos/Slides/img%20(102).jpg"
          data-lazy-delay="1000"
          data-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
          data-lazy-animation="shake"
          alt="Example image"
          class="img-fluid lazy"
        />
      

Via JavaScript


        const lazyElement = new mdb.LazyLoad(document.getElementById('lazyElement'), { 
          lazySrc: '...',
          lazyPlaceholder: '...', 
          lazyError: '...', 
        });
      

Via jQuery

Note: By default, MDB does not include jQuery and you have to add it to the project on your own.


        $('.example-class').lazyLoad(options);
      

Options

Name Type Default Description
lazy-src String '...' Defines the source of an element
lazy-delay Number 500 Defines delay after which element will show
lazy-animation String 'fade-in' Defines animation during element showing
lazy-offset Number 0 Defines an additional offset after which element will show
lazy-placeholder String - Defines a picture that is shown before loading a proper element
lazy-error String - Defines a picture that is shown if an error with showing element occurs

Methods

Name Description Example
dispose Removes an instance of the lazy element lazyElement.dispose()

        mdb.lazyLoad.getInstance(document.getElementById('example-element')).dispose();
      

Events

Name Description
onLoad.mdb.lazy This event fires immediately when an image is loaded.
onError.mdb.lazy This event fires immediately when an error with loading image occurs.

        const lazyLoadInstance = mdb.lazyLoad.getInstance(document.getElementById('example-element'));
        lazyLoadInstance.addEventListener('onLoad.mdb.lazy', () => { // do something });
      

Import

MDB UI KIT also works with module bundlers. Use the following code to import this component:


        import { LazyLoad } from 'mdb-ui-kit';