What is native lazy loading in browsers?

What is native lazy loading?

Lazy loading is a technique that delays the loading of portions of a web page until they are needed. This is done by inserting a loading indicator until the content is loaded.

Native lazy loading is a technique that delays the loading of assets until they are needed. This is done by inserting a loading indicator until the assets are loaded. Native lazy loading is supported in browsers that have a native asynchronous module loading (AMD) mechanism.

Browsers that support native lazy loading

Currently, native lazy loading is supported in Chrome, Firefox, and Safari.

Native lazy loading in Chrome

To enable native lazy loading in Chrome, you need to enable the Async Module Loading flag in the chrome://flags page.

Native lazy loading in Firefox

To enable native lazy loading in Firefox, you need to enable the Async Module Loading flag in the about:config page.

Native lazy loading in Safari

To enable native lazy loading in Safari, you need to enable the Enable lazy module loading flag in the WebKit.Preference.LazyLoad page.

Usage

To use native lazy loading in your web page, you need to include the lazy-loader.js script. This script will load the lazy-loader.css style sheet.

The lazy-loader.js script will insert a loading indicator until the assets are loaded. Once the assets are loaded, the loading indicator will disappear.

var lazyLoader = new LazyLoader(); 

lazyLoader.init({ 
    el: '[data-src]', 
    css: 'lazy-loader.css' 
});

The lazy-loader.css style sheet contains the styles for the loading indicator.

.lazy-loader { 
    background-color: #fff; 
    opacity: 0.8; 
}

You can also customize the loading indicator by setting the "loading" and "loaded" classes.

.lazy-loader.loading { 
    background-color: 
        #fff; opacity: 1; 
} 

.lazy-loader.loaded { 
    background-color: #000; 
    opacity: 0; 
}

Native lazy loading in other browsers

Although native lazy loading is not supported in all browsers, you can still use the lazy-loader.js script to load the content asynchronously. This script will use the evalAsync() function to load the content asynchronously.

var lazyLoader = new LazyLoader(); 
lazyLoader.init({ 
    el: '[data-src]', 
    css: 'lazy-loader.css' 
});

The lazy-loader.css style sheet contains the styles for the loading indicator.

.lazy-loader { 
    background-color: #fff; 
    opacity: 0.8; 
}

The evalAsync() function will execute the code asynchronously.

evalAsync(function() { // code to load asynchronously });