Convert Figma logo to code with AI

desandro logoimagesloaded

:camera: JavaScript is all like "You images done yet or what?"

8,879
1,146
8,879
16

Top Related Projects

JavaScript image gallery for mobile and desktop, modular, framework independent

A customizable, modular, responsive, lightbox gallery plugin.

JavaScript image viewer.

39,745

Most modern mobile touch slider with hardware accelerated transitions

jQuery lightbox script for displaying images, videos and more. Touch enabled, responsive and fully customizable.

Quick Overview

imagesLoaded is a JavaScript library that detects when images have been loaded on a web page. It provides a simple way to handle events when all images within a container or on an entire page have finished loading, which is particularly useful for creating responsive layouts or implementing lazy loading techniques.

Pros

  • Easy to use with a straightforward API
  • Works with both <img> elements and background images
  • Supports modern module systems (ES6, CommonJS) and can be used as a jQuery plugin
  • Lightweight and has no dependencies

Cons

  • Limited functionality beyond image loading detection
  • May not be necessary for simple websites with few images
  • Requires manual integration with other libraries for advanced image handling
  • Not actively maintained (last release in 2019)

Code Examples

Loading images in a container:

imagesLoaded('#container', function() {
  console.log('All images in #container have loaded');
});

Using with vanilla JavaScript:

const elem = document.querySelector('#container');
imagesLoaded(elem, function() {
  console.log('All images have loaded');
});

Event-based approach:

const imgLoad = imagesLoaded('body');
imgLoad.on('progress', function(instance, image) {
  const result = image.isLoaded ? 'loaded' : 'broken';
  console.log('Image is ' + result + ' for ' + image.img.src);
});

Getting Started

  1. Install imagesLoaded via npm:
npm install imagesloaded
  1. Import and use in your JavaScript file:
import imagesLoaded from 'imagesloaded';

// Basic usage
imagesLoaded('#container', function() {
  console.log('All images have loaded');
});

// With options and events
const imgLoad = imagesLoaded('#container', { background: true });
imgLoad.on('always', function() {
  console.log('All images have loaded or failed');
});

For browser usage without a module bundler, include the script in your HTML:

<script src="https://unpkg.com/imagesloaded@5/imagesloaded.pkgd.min.js"></script>

Then use it in your JavaScript as shown in the examples above.

Competitor Comparisons

JavaScript image gallery for mobile and desktop, modular, framework independent

Pros of PhotoSwipe

  • Full-featured image gallery with zoom, swipe, and sharing capabilities
  • Responsive design that works well on mobile devices
  • Customizable UI and extensive API for advanced implementations

Cons of PhotoSwipe

  • Larger file size and more complex setup compared to imagesLoaded
  • May be overkill for simple image loading tasks
  • Steeper learning curve for basic implementations

Code Comparison

imagesLoaded:

imagesLoaded( elem, callback )
imagesLoaded( elem ).on( 'progress', function( instance, image ) {
  var result = image.isLoaded ? 'loaded' : 'broken';
  console.log( 'image is ' + result + ' for ' + image.img.src );
});

PhotoSwipe:

var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();

gallery.listen('imageLoadComplete', function(index, item) {
  // index - index of a slide that was loaded
  // item - slide object
});

Summary

While imagesLoaded focuses solely on detecting when images have finished loading, PhotoSwipe provides a complete gallery solution with advanced features. imagesLoaded is lighter and easier to implement for basic image loading tasks, while PhotoSwipe offers a rich user experience at the cost of increased complexity and file size. Choose based on your specific project requirements and desired functionality.

A customizable, modular, responsive, lightbox gallery plugin.

Pros of lightGallery

  • More feature-rich, offering a complete gallery solution with various plugins
  • Supports multiple media types (images, videos, iframes)
  • Responsive design with touch support for mobile devices

Cons of lightGallery

  • Larger file size due to additional features
  • Steeper learning curve for advanced customization
  • May be overkill for simple image loading tasks

Code Comparison

imagesLoaded:

imagesLoaded( elem, callback )
// or
imagesLoaded( elem ).on( 'progress', onProgress );

lightGallery:

lightGallery(document.getElementById('lightgallery'), {
    plugins: [lgZoom, lgThumbnail],
    speed: 500
});

Summary

While imagesLoaded focuses solely on detecting when images have been loaded, lightGallery provides a comprehensive gallery solution with advanced features. imagesLoaded is lightweight and straightforward, making it ideal for simple image loading tasks. On the other hand, lightGallery offers a rich set of features for creating interactive galleries but comes with a larger file size and more complex setup. The choice between the two depends on the specific requirements of your project, whether you need a simple image loading utility or a full-fledged gallery solution.

JavaScript image viewer.

Pros of ViewerJS

  • Offers a full-featured image viewer with zoom, rotate, and fullscreen capabilities
  • Supports touch gestures for mobile devices
  • Provides a more comprehensive solution for image viewing and manipulation

Cons of ViewerJS

  • Larger file size and more complex implementation compared to ImagesLoaded
  • May be overkill for simple image loading scenarios
  • Requires additional CSS for styling the viewer interface

Code Comparison

ImagesLoaded:

imagesLoaded( elem, callback )
imagesLoaded( elem ).on( 'progress', onProgress )

ViewerJS:

const viewer = new Viewer(document.getElementById('images'));
viewer.show();
viewer.zoom(0.5);
viewer.rotate(90);

ImagesLoaded focuses on detecting when images have finished loading, while ViewerJS provides a full-featured image viewer with various manipulation options. ImagesLoaded is more lightweight and suitable for simple image loading detection, whereas ViewerJS offers a complete solution for image viewing and interaction but with a larger footprint and more complex implementation.

39,745

Most modern mobile touch slider with hardware accelerated transitions

Pros of Swiper

  • More feature-rich, offering advanced functionality like pagination, navigation, and effects
  • Highly customizable with a wide range of options and modules
  • Responsive and touch-friendly, ideal for mobile devices

Cons of Swiper

  • Larger file size and potentially higher performance overhead
  • Steeper learning curve due to its extensive API and configuration options
  • May be overkill for simple image loading tasks

Code Comparison

ImagesLoaded:

imagesLoaded( elem, callback )
imagesLoaded( elem ).on( 'progress', onProgress )

Swiper:

const swiper = new Swiper('.swiper-container', {
  slidesPerView: 3,
  spaceBetween: 30,
  pagination: {
    el: '.swiper-pagination',
    clickable: true,
  },
});

Summary

ImagesLoaded is a lightweight library focused solely on detecting when images have been loaded, making it ideal for simple image-related tasks. Swiper, on the other hand, is a full-featured slider library with extensive customization options, touch support, and advanced functionality. While Swiper offers more possibilities for creating interactive slideshows, ImagesLoaded is more suitable for basic image loading detection without the additional overhead.

jQuery lightbox script for displaying images, videos and more. Touch enabled, responsive and fully customizable.

Pros of Fancybox

  • Provides a complete lightbox solution with built-in gallery functionality
  • Offers touch-enabled, responsive design out of the box
  • Includes various animation effects and customization options

Cons of Fancybox

  • Larger file size and more complex implementation compared to ImagesLoaded
  • May be overkill for simple image loading tasks
  • Requires additional CSS and JavaScript files

Code Comparison

ImagesLoaded:

imagesLoaded( elem, callback )
imagesLoaded( elem ).on( 'progress', onProgress )

Fancybox:

Fancybox.show([{ src: "image.jpg", caption: "My Caption" }]);
Fancybox.bind("[data-fancybox]", { /* options */ });

Summary

ImagesLoaded is a lightweight library focused solely on detecting when images have been loaded, making it ideal for simple image loading tasks. Fancybox, on the other hand, is a full-featured lightbox solution that offers more functionality but comes with a larger footprint and more complex implementation. The choice between the two depends on the specific needs of your project, with ImagesLoaded being better suited for basic image loading detection and Fancybox excelling in creating interactive image galleries and lightboxes.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

imagesLoaded

JavaScript is all like "You images done yet or what?"

imagesloaded.desandro.com

Detect when images have been loaded.

Install

Download

CDN

<script src="https://unpkg.com/imagesloaded@5/imagesloaded.pkgd.min.js"></script>
<!-- or -->
<script src="https://unpkg.com/imagesloaded@5/imagesloaded.pkgd.js"></script>

Package managers

Install via npm: npm install imagesloaded

Install via Yarn: yarn add imagesloaded

jQuery

You can use imagesLoaded as a jQuery Plugin.

$('#container').imagesLoaded( function() {
  // images have loaded
});

// options
$('#container').imagesLoaded( {
  // options...
  },
  function() {
    // images have loaded
  }
);

.imagesLoaded() returns a jQuery Deferred object. This allows you to use .always(), .done(), .fail() and .progress().

$('#container').imagesLoaded()
  .always( function( instance ) {
    console.log('all images loaded');
  })
  .done( function( instance ) {
    console.log('all images successfully loaded');
  })
  .fail( function() {
    console.log('all images loaded, at least one is broken');
  })
  .progress( function( instance, image ) {
    var result = image.isLoaded ? 'loaded' : 'broken';
    console.log( 'image is ' + result + ' for ' + image.img.src );
  });

Vanilla JavaScript

You can use imagesLoaded with vanilla JS.

imagesLoaded( elem, callback )
// options
imagesLoaded( elem, options, callback )
// you can use `new` if you like
new imagesLoaded( elem, callback )
  • elem Element, NodeList, Array, or Selector String
  • options Object
  • callback Function - function triggered after all images have been loaded

Using a callback function is the same as binding it to the always event (see below).

// element
imagesLoaded( document.querySelector('#container'), function( instance ) {
  console.log('all images are loaded');
});
// selector string
imagesLoaded( '#container', function() {...});
// multiple elements
var posts = document.querySelectorAll('.post');
imagesLoaded( posts, function() {...});

Bind events with vanilla JS with .on(), .off(), and .once() methods.

var imgLoad = imagesLoaded( elem );
function onAlways( instance ) {
  console.log('all images are loaded');
}
// bind with .on()
imgLoad.on( 'always', onAlways );
// unbind with .off()
imgLoad.off( 'always', onAlways );

Background

Detect when background images have loaded, in addition to <img>s.

Set { background: true } to detect when the element's background image has loaded.

// jQuery
$('#container').imagesLoaded( { background: true }, function() {
  console.log('#container background image loaded');
});

// vanilla JS
imagesLoaded( '#container', { background: true }, function() {
  console.log('#container background image loaded');
});

See jQuery demo or vanilla JS demo on CodePen.

Set to a selector string like { background: '.item' } to detect when the background images of child elements have loaded.

// jQuery
$('#container').imagesLoaded( { background: '.item' }, function() {
  console.log('all .item background images loaded');
});

// vanilla JS
imagesLoaded( '#container', { background: '.item' }, function() {
  console.log('all .item background images loaded');
});

See jQuery demo or vanilla JS demo on CodePen.

Events

always

// jQuery
$('#container').imagesLoaded().always( function( instance ) {
  console.log('ALWAYS - all images have been loaded');
});

// vanilla JS
imgLoad.on( 'always', function( instance ) {
  console.log('ALWAYS - all images have been loaded');
});

Triggered after all images have been either loaded or confirmed broken.

  • instance imagesLoaded - the imagesLoaded instance

done

// jQuery
$('#container').imagesLoaded().done( function( instance ) {
  console.log('DONE  - all images have been successfully loaded');
});

// vanilla JS
imgLoad.on( 'done', function( instance ) {
  console.log('DONE  - all images have been successfully loaded');
});

Triggered after all images have successfully loaded without any broken images.

fail

$('#container').imagesLoaded().fail( function( instance ) {
  console.log('FAIL - all images loaded, at least one is broken');
});

// vanilla JS
imgLoad.on( 'fail', function( instance ) {
  console.log('FAIL - all images loaded, at least one is broken');
});

Triggered after all images have been loaded with at least one broken image.

progress

imgLoad.on( 'progress', function( instance, image ) {
  var result = image.isLoaded ? 'loaded' : 'broken';
  console.log( 'image is ' + result + ' for ' + image.img.src );
});

Triggered after each image has been loaded.

  • instance imagesLoaded - the imagesLoaded instance
  • image LoadingImage - the LoadingImage instance of the loaded image

Properties

LoadingImage.img

Image - The img element

LoadingImage.isLoaded

Boolean - true when the image has successfully loaded

imagesLoaded.images

Array of LoadingImage instances for each image detected

var imgLoad = imagesLoaded('#container');
imgLoad.on( 'always', function() {
  console.log( imgLoad.images.length + ' images loaded' );
  // detect which image is broken
  for ( var i = 0, len = imgLoad.images.length; i < len; i++ ) {
    var image = imgLoad.images[i];
    var result = image.isLoaded ? 'loaded' : 'broken';
    console.log( 'image is ' + result + ' for ' + image.img.src );
  }
});

Webpack

Install imagesLoaded with npm.

npm install imagesloaded

You can then require('imagesloaded').

// main.js
var imagesLoaded = require('imagesloaded');

imagesLoaded( '#container', function() {
  // images have loaded
});

Use .makeJQueryPlugin to make .imagesLoaded() jQuery plugin.

// main.js
var imagesLoaded = require('imagesloaded');
var $ = require('jquery');

// provide jQuery argument
imagesLoaded.makeJQueryPlugin( $ );
// now use .imagesLoaded() jQuery plugin
$('#container').imagesLoaded( function() {...});

Run webpack.

webpack main.js bundle.js

Browserify

imagesLoaded works with Browserify.

npm install imagesloaded --save
var imagesLoaded = require('imagesloaded');

imagesLoaded( elem, function() {...} );

Use .makeJQueryPlugin to make to use .imagesLoaded() jQuery plugin.

var $ = require('jquery');
var imagesLoaded = require('imagesloaded');

// provide jQuery argument
imagesLoaded.makeJQueryPlugin( $ );
// now use .imagesLoaded() jQuery plugin
$('#container').imagesLoaded( function() {...});

Browser support

  • Chrome 49+
  • Firefox 41+
  • Edge 14+
  • iOS Safari 8+

Use imagesLoaded v4 for Internet Explorer and other older browser support.

Development

Development uses Node.js v16 with npm v8

nvm use

MIT License

imagesLoaded is released under the MIT License. Have at it.

NPM DownloadsLast 30 Days