desandro /imagesloaded

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

★ 8,836⑂ 1,105👁 223⑃ 68● 247▤ 5
JavaScriptMITCreated 2011-08-25imagesloaded.desandro.com
More from this author
Download: v4.1.4imagesloaded-4.1.4 ↓4.1.42018-01-02
+0
Daily
8,836
stars
1,105
forks
223
watchers
68
prs
247
issues
5
releases

README

imagesLoaded

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

imagesloaded.desandro.com

Detect when images have been loaded.

 Install

 Download

+ imagesloaded.pkgd.min.js minified
+ imagesloaded.pkgd.js un-minified

 CDN

 html





 Package managers

Install via npm: npm install imagesloaded

Install via Yarn: yarn add imagesloaded

 jQuery

You can use imagesLoaded as a jQuery Plugin.

 js
$('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().

 js
$('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.

 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).

 js
// 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.

 js
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 s.

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

 js
// 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.

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

// vanilla JS
imagesLoaded( 'con

Top issues

Latest release

v4.1.4 v4.1.4 2018-01-02
Check for non-zero naturalWidth; Fixes bug with metafizzy/infinite-scroll671

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON