Let me share with you nine JavaScript image lazy loading libraries
javascriptThe column introduces several image lazy loading libraries.
Related free learning recommendations: javascript (video)
Why do we need lazy loading of images
Performance is critical in web applications. You can have the most beautiful and attractive website in the world, but if it doesn’t load quickly on a browser, people will tend to skip it. Getting your website to perform really well can be quite tricky. This is because there are many bottlenecks in web development, such as expensive JavaScript, slow web font display, bulky image resources, etc.
In this article we mainly focus on the impact of image resources on the website. According to Jecelyn research, a webpage consumes an average of 5MB of data just to load images. This can be a heavy burden for users as mobile data is very expensive in some countries. Users also experience issues with sites taking too long to load, especially on slow connections. These can have a negative impact on your website.
According to Jakob Nielson research, here are some important statistics you should keep in mind:
- Website load times below 100 milliseconds are considered instantaneous.
- Latency between 100 and 300 milliseconds is perceptible.
- 47% of consumers expect web pages to load in two seconds or less.
- 40% of consumers will wait no more than 3 seconds to load before abandoning a website.
What is lazy loading?
There are several strategies that can provide efficient services for the image resources of the website without affecting performance and quality, and lazy loading is one of them. Lazy loading means loading only what is needed and deferring the rest until needed. This strategy can be applied to images, videos, text, and other types of data. But mostly, it's suitable for bulky content like image assets.
There are several ways to implement lazy loading of images on your website. For example, you can use the Intersection Observer API
, or use an event handler to determine whether an element is in view. There are also several powerful JavaScript libraries. You can use the following image lazy loading library methods according to your needs and compatibility. Let’s take a look!
Lazy Sizes
Lazy Sizes is one of the best lazy loading libraries currently, with more than 14.1K collections on Github, and only 3.4kB after compression. It also supports approximately 98.5% of browser users, and its documentation is written in an easy-to-understand manner.
Features
- Contains support for responsive images.
- Optimize SEO by detecting search engines with the help of user agents and loading all images instantly.
- Based on efficient and practical code.
- Preload resources when the network connection is idle.
- Contains support for LQIPs.
- Supports
IntersectionObserver
,MutationObserver
andgetElementsByClassName
etc. - Supports the use of plug-ins to extend features.
- Supports automatic calculation of response image size.
You can see examples here.
Lozad.js
Lozad.js supports lazy loading of images, iframe
, ads, videos and other elements. It has nearly 6.4K collections on Github and is very popular in the community. According to the research team, this library is used by web applications of several brands, including Tesla, Domino, Xiaomi, and the BBC. It's very small, only 1.1kB gzipped. Since it uses the IntersectionObserver
API and the MutationObserver
API, it supports around 92% of browser users.
Features
- There are no dependencies.
- Support lazy loading of dynamically added elements.
- Uses entirely JavaScript.
- Contains support for LQIPs and response images.
- More efficient than libraries using
getBoundingClientRect()
. - Polyfills can be used on unsupported browsers.
You can see examples here.
Tuupola’s Lazyload
Tuupola’s Lazyload is another popular image lazy loading library on Github, with nearly 8.4K collections. It uses the IntersectionObserver
API and is simple and easy to use. It is only 956 bytes after compression, which is smaller than other libraries. This can be attributed to the fact that it only uses the IntersectionObserver
API, as other libraries use other combinations to achieve better compatibility and performance. Furthermore, thanks to this, 92% of browser users currently support it.
Features
- It includes a jQuery wrapper for convenience.
- Includes support for LQIPs and response images.
- The core
IntersectionObserver
API can be configured by passing additional parameters.
Vanilla Lazyload by Andrea Verlicchi
Vanilla lazy load is another pure JavaScript library for lazy loading of images, videos and iframes. It is very popular on Github, with nearly 1500 repositories and packages available. It has over 1.9 million downloads per year on NPM. It's only 2.7kB compressed. Similar to other libraries, this library uses the IntersectionObserver
API, which is supported by 92% of browser users.
- SEO friendly as the library does not cover images from search engines.
- Supports unstable network connections as the library will automatically reload images after the connection is interrupted.
- If the image exits the viewport, cancel loading of the image.
- Contains support for LQIPs and response images.
- Uses entirely JavaScript.
#You can see an example here.
Yall.js
Yall.js is another JavaScript library that also only uses the IntersectionObserver
API to lazy load images, videos, iframe
and CSS background image. This library has approximately 1.1K collections and is used by 91 users in their project libraries. This library can be compressed to 1kB. As we saw with previous libraries, Yall.js also supports 92% of browser users because of the use of the IntersectionObserver
API. It is important to note that if the browser does not support the IntersectionObserver
API, there will be no backup. In that case you must use polyfill
.
Features
- Supports the detection of dynamically loaded elements with the help of
MutationObserver
API. - Use the
requestIdleCallback
method to optimize browser idle time. - Supports direct implementation of LQIP through the
src
attribute. - Support lazy loading of CSS background.
Layzr.js
Layzr.js is a lightweight image lazy loading library based on JavaScript. It mainly uses Element.classList
, with few ES5 array methods and requestAnimationFrame
methods. Thanks to these APIs, more than 97% of browser users support this library. Layzr.js has more than 5.6K collections on Github and is very popular. It is only 1kB after compression.
- There are no dependencies.
- Intelligent selection of image sources based on browser compatibility and availability.
- Support dynamically added elements.
- Clear and concise documentation and examples.
- Viewport adjustment images with threshold properties are lazy loaded, allowing images to be loaded earlier or later as needed.
You can see examples here.
Blazy.js
Blazy.js is another lightweight JavaScript lazy loading library that can handle images, videos and iframe
. It is very popular on Github with 2.6K collections and is currently used by over 860 open source project libraries. It's only 1.9kB compressed.
Use the Element.getBoundingClientRect()
method, which may not perform as well as other libraries implementing the IntersectionObserver
API. But due to this approach, this library is supported by more than 98% of browser users. It also uses Element.closest()
. Browser support for this API is just over 94%. In this case, you don't have to worry about the missing 6% because the library contains a polyfill
for unsupported browsers.
Features
- Used on real websites with millions of monthly visits.
- There are no dependencies.
- Support responsive images.
- Similar to Layzr.js allows loading elements with offsets.
- Clear documentation with example code.
- Supports module formats such as AMD, CommonJS and globals.
- Very easy to provide retina pictures.
You can see examples here.
Responsively Lazy
Responsively lazy is also a lazy loading library for images. Its content is concise, only 1.1kB compressed. Because of its good syntax implementation, it stands out from many libraries. Most of the libraries we discussed above require you to use the noscript
tag for browsers that disable javascript, ignore the src
attribute, etc. But lazy can use the traditional src
attribute and add srcset
and src
attributes for supported browsers. This makes this library search engine optimization (SEO) friendly. This library also uses Element.getBoundingClientRect()
so forced layout reflows will also occur in this library.
In addition, this library has nearly 1.1K collections on Github, and almost 95% of browser users support this library.
Features
- Support responsive images.
- Support webp.
- Search Engine Optimization (SEO) friendly.
- Not many customizations available.
You can see examples here.
LazyestLoad.js
LazyestLoad.js is one of the smallest libraries on this list. It's only 700 bytes, compressed to just 639 bytes. There are two versions of this library, lazyload
and lazyestload
. They all have different uses, the lazyload
version works like the normal library, the image will load when it is about to enter the viewport; however the lazyestload
version only loads when the user stops scrolling and the image The image will only load if it is in the viewport or within 100 pixels. This helps reduce network load if the user is just scrolling without pausing to see the image.
It mainly uses the Element.getBoundingClientRect()
method, which is not efficient compared to other implementations and is also known to trigger layout rearrangements.
This library only handles images, unlike other libraries that can handle videos and iframe
. It also has over 15,000 collections on Github.
Features
- Simple and straightforward.
- Does not allow extensive customization like other libraries.
- Support responsive images.
- The documentation is not detailed enough.
You can view the lazyload example and view the lazyestload example.
As most modern browsers will support native lazy loading, it is recommended to use native implementations. Native lazy loading also ensures that images load lazily even if JavaScript is disabled in the browser. Just use the loading="lazy"
attribute in the img
tag and save yourself all the trouble.
Most modern browsers support native lazy loading, and Safari will soon support it. Currently, the browser support rate is 74%. If the browser does not support the native implementation, you can use polyfill or one of the above lazy loading libraries.
To be safe, you may still need to use dynamic imports to implement one of the libraries.
Know your target audience
If you carefully analyze all the above given libraries, you will find that they compete fiercely in three aspects: performance, size and browser compatibility ( user coverage). These usually have to sacrifice at least one to improve the level of the other.
For example, if you use a library that implements the IntersectionObserver
API, you will get a high-performance library, but its user coverage will be smaller. If patching is required, there needs to be a fallback option, such as polyfills
, which will increase the overall size of the library.
In another example, if the lazy loading library uses the getBoundingClientRect()
method, it will not perform as well as the IntersectionObserver
API because it is known to have forced layout reflow issues . Although performance is sacrificed, user coverage will be higher than the former. Hope I can make this clear.
How to minimize compatibility issues and maximize performance?
These areas can be improved by understanding your target audience and their browser usage. If you know your target audience and the browsers they use, you can ensure that your implementation of lazy loading is better suited for those browser versions. This will reduce the need to include polyfill
for unsupported browsers since it will already be known which browsers need to be concerned. When you have an outlier (unsupported browser), images can load directly without any lag or delay. If you have a good understanding of your audience, the number of these outliers will be negligible.
This approach will help to use an implementation library that performs well, keeps the library size to a minimum by ignoring browser exceptions, and supports the target user's browser version.
This article briefly discusses JavaScript’s lazy loading library and some methods to improve efficiency and user experience. Have your say in the comments below.
Thanks for reading and happy coding! !
The above is the detailed content of Let me share with you nine JavaScript image lazy loading libraries. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
