imagesLoaded is a small, fast, and robust JavaScript library that provides a callback mechanism once all images within a given container have fully loaded. This is particularly useful for situations where you need to perform actions (like resizing a layout, revealing hidden content, or triggering animations) that depend on image dimensions being available. Unlike simply checking for onload
on each image, imagesLoaded accounts for images that fail to load and handles image caching effectively.
Using imagesLoaded offers several key advantages:
onload
event listeners can be unreliable, particularly with cached images.imagesLoaded is available via npm and yarn. You can also include it directly via a CDN.
Using npm or yarn:
npm install imagesloaded
# or
yarn add imagesloaded
Then, import it into your JavaScript file:
import imagesLoaded from 'imagesloaded';
Using a CDN (e.g., jsDelivr):
Include the following <script>
tag in the <head>
of your HTML file:
<script src="https://cdn.jsdelivr.net/npm/imagesloaded@5/imagesloaded.pkgd.min.js"></script>
This example shows how to use imagesLoaded to wait for all images within a container with the ID “my-images” to load before displaying a message.
import imagesLoaded from 'imagesloaded';
const imageContainer = document.getElementById('my-images');
imagesLoaded(imageContainer, function() {
console.log('All images loaded!');
// Perform actions that depend on image dimensions here, e.g.,
document.getElementById('loading-message').style.display = 'none';
; })
Remember to include the imagesloaded.js
file (either via npm/yarn or CDN) before this JavaScript code in your HTML. This example utilizes an element with the ID “loading-message” that will be hidden after all images have loaded. Replace this with your own logic as needed. The imageContainer
can be any DOM element containing images. It doesn’t have to be a <div>
. It could even be the <body>
element itself if you want to wait for all images on the page to load.
The core functionality of imagesLoaded centers around the imagesLoaded
function. This function takes a DOM element as its first argument (the container element containing images) and a callback function as its second. The callback function is executed only after all images within the container have fully loaded.
The imagesLoaded
function intelligently handles various scenarios:
fail
event (detailed below).progress
, done
, fail
)The imagesLoaded
function, while primarily using a callback, also offers a more flexible approach using events. This allows for finer-grained control over the loading process.
Instead of directly passing a callback function to imagesLoaded
, you can create an instance of the imagesLoaded
class and then listen for events. This provides more sophisticated control over event handling. The events emitted are:
progress
: This event is fired for each image as it loads. The event object contains information about the loaded image, including its img
element and its isLoaded
status. This allows you to track the progress of the image loading process.
done
: This event is triggered only after all images within the specified container have successfully loaded.
fail
: This event is triggered if one or more images fail to load. The event data provides information about which images failed.
Here’s an example using the event-based approach:
import imagesLoaded from 'imagesloaded';
const imageContainer = document.getElementById('my-images');
const instance = imagesLoaded(imageContainer);
.on( 'progress', function( instance, image ) {
instanceconsole.log( 'image loaded:', image.img );
;
})
.on( 'done', function() {
instanceconsole.log( 'All images loaded!' );
;
})
.on( 'fail', function( instance, image ) {
instanceconsole.log( 'Image failed to load:', image.img.src);
; })
imagesLoaded inherently supports various image formats without any additional configuration. It correctly identifies and handles the loading of JPEG, PNG, GIF, SVG, and other common image types. You don’t need to perform any special checks or pre-processing for different image formats.
imagesLoaded
actually exists and contains the images you expect. Inspect it in your browser’s developer tools to ensure it’s properly structured.If you are still experiencing issues, please provide details including your code, the browser you are using, and the specific error message (if any) for more targeted assistance.
While imagesLoaded is a vanilla JavaScript library, integrating it into popular frameworks like React, Angular, and Vue is straightforward. The core principle remains the same: you need to select the container element and then use the imagesLoaded
function or event system. However, the specifics of how you select the element and handle the callback will depend on the framework.
React:
import React, { useEffect, useState } from 'react';
import imagesLoaded from 'imagesloaded';
function MyComponent() {
const [imagesLoadedStatus, setImagesLoadedStatus] = useState(false);
const imageContainerRef = useRef(null);
useEffect(() => {
if (imageContainerRef.current) {
imagesLoaded(imageContainerRef.current).on('done', () => {
setImagesLoadedStatus(true);
;
})
}, []);
}
return (
<div ref={imageContainerRef}>
{/* Your images here */}
{imagesLoadedStatus && <p>All images loaded!</p>}
</div>
;
) }
Angular:
You can use ViewChild
or ElementRef
to access the container element within your Angular component, and then call imagesLoaded
within ngOnInit
or a similar lifecycle hook.
Vue:
You can use a ref
to access the container element in your Vue template and then call imagesLoaded
within a mounted
lifecycle hook or a watcher.
In all these framework examples, the crucial part is ensuring that the imagesLoaded
call is made after the DOM element containing the images has been rendered. Framework-specific lifecycle hooks or asynchronous operations (like useEffect
in React) are essential for proper timing.
While imagesLoaded automatically handles most scenarios, you can customize certain aspects:
Timeout: If you need to adjust the timeout period for images that fail to load, you can do so in newer versions (although the default should suffice in most cases). Refer to the updated documentation for details on adjusting the timeout.
Debugging: Enabling debugging can provide more verbose logging for troubleshooting purposes, again this functionality might depend on version and isn’t always available. Check the latest documentation for any debugging flags or options.
imagesLoaded primarily focuses on images that are directly part of the DOM (<img>
tags). To handle background images, you’ll need a different approach, as imagesLoaded does not directly detect the loading of background images. Consider using the onload
event listener on a hidden <img>
tag that points to the background image URL to track when the background image has fully loaded.
For exceptionally large numbers of images, optimizing the loading process becomes crucial. Consider these strategies:
Beyond the strategies already mentioned for large numbers of images, other general performance improvements apply to imagesLoaded usage:
done
event fires.progress
, done
, and fail
events instead of a simple callback function allows more efficient control and minimizes potential overhead.Remember to profile your application to identify specific performance bottlenecks. Using browser developer tools is key to pinpointing areas for optimization.
imagesLoaded(elements, options)
The core function of the imagesLoaded library. It takes two arguments:
elements
: This is the first and required argument. It can be one of the following:
<div>
). imagesLoaded will then check for all images within that element.'.my-images'
) to select multiple elements containing images. This uses document.querySelectorAll
under the hood.options
(optional): An object containing optional settings to customize the behavior of imagesLoaded. Details on the options
object are provided in the next section. If omitted, imagesLoaded uses its default settings.Return Value:
The function returns an imagesLoaded
instance. This object allows you to listen for events (progress
, done
, fail
) using the .on()
method. In older versions a simple callback was provided as the second argument. The newer event system provides more flexibility.
Example:
import imagesLoaded from 'imagesloaded';
const myContainer = document.querySelector('.my-image-container');
const imgArray = [document.getElementById('image1'), document.getElementById('image2')];
// Using a single element
const instance1 = imagesLoaded( myContainer );
.on('done', () => console.log('All images in myContainer loaded!') );
instance1
// Using an array of elements
const instance2 = imagesLoaded( imgArray );
.on('done', () => console.log('All images in the array loaded!') );
instance2
// Using a selector string
const instance3 = imagesLoaded( '.my-other-images' );
.on('done', () => console.log('All images in elements with the class my-other-images loaded!') ); instance3
The imagesLoaded
instance returned by the imagesLoaded()
function exposes the .on()
method to attach event listeners. Available events are:
progress
: Fired for each image as it loads. The callback function receives two arguments: instance
(the imagesLoaded
instance itself) and image
(an object containing information about the loaded image, such as its img
element and its isLoaded
status (true/false)).
done
: Fired after all images within the specified elements have loaded successfully. The callback function receives one argument: instance
(the imagesLoaded
instance).
fail
: Fired if one or more images fail to load. The callback function receives two arguments: instance
and image
(an object containing information about the failed image, similar to the progress
event).
Example using .on()
:
const instance = imagesLoaded(myContainer);
.on( 'progress', (instance, image) => {
instanceconsole.log(`Image loaded: ${image.img.src}`);
;
} )
.on( 'done', () => {
instanceconsole.log('All images loaded!');
;
} )
.on( 'fail', (instance, image) => {
instanceconsole.log(`Image failed to load: ${image.img.src}`);
; } )
The options
object (second argument to imagesLoaded()
) allows for fine-grained control over the loading process. While not all options are available in all versions, and newer options might have replaced older ones, the following are representative:
background
(boolean): (Potentially deprecated or version dependent) This may have been used in older versions to specifically handle background images. Check current documentation for newer background image handling strategies. Currently, background images are not directly supported.
timeout
(number, milliseconds): (May have limited or no effect in recent versions) Specifies a timeout in milliseconds after which images that haven’t loaded will be considered failed. Check if this setting is even relevant in the version you use. Modern implementations likely handle timeouts more intelligently. This option might not exist in newer versions.
It’s crucial to consult the most up-to-date documentation for the specific version of imagesLoaded you are using, as the API and available options may have changed over time. The options listed here are illustrative examples and may not represent all options available in all versions.
We welcome contributions to imagesLoaded! Whether you’re reporting a bug, suggesting a feature, or submitting a pull request, your involvement is valuable.
Before creating a new issue, please search existing issues to see if the problem has already been reported. If you can’t find a matching issue, please create a new one providing the following information:
If you’d like to contribute code, follow these steps:
fix-bug-123
, feature-new-option
).To maintain consistency and readability, please adhere to the following coding style guidelines when contributing to imagesLoaded:
We use ESLint to enforce code style. It’s recommended to install ESLint and configure it according to the project’s .eslintrc
file before submitting a pull request. This will help you identify and fix style issues before they are submitted. The project maintainers might reject pull requests that do not follow the styling guide.