imagesloaded jquery plugin fork
JavaScript is all like "You images done yet or what?", (*1)
imagesloaded.desandro.com, (*2)
Detect when images have been loaded., (*3)
``` html , (*4)
### Package managers Install via [npm](https://www.npmjs.com/package/imagesloaded): `npm install imagesloaded` Install via [Bower](http://bower.io): `bower install imagesloaded --save` ## 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()
., (*5)
``` 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 ); });, (*6)
## 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 loadedUsing a callback function is the same as binding it to the always
event (see below)., (*7)
``` 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() {...});, (*8)
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 );
Detect when background images have loaded, in addition to <img>
s., (*9)
Set { background: true }
to detect when the element's background image has loaded., (*10)
``` js // jQuery $('#container').imagesLoaded( { background: true }, function() { console.log('#container background image loaded'); });, (*11)
// vanilla JS imagesLoaded( '#container', { background: true }, function() { console.log('#container background image loaded'); });, (*12)
[See jQuery demo](http://codepen.io/desandro/pen/pjVMPB) or [vanilla JS demo](http://codepen.io/desandro/pen/avKooW) 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( '#container', { background: '.item' }, function() { console.log('all .item background images loaded'); });
See jQuery demo or vanilla JS demo on CodePen., (*13)
``` js // jQuery $('#container').imagesLoaded().always( function( instance ) { console.log('ALWAYS - all images have been loaded'); });, (*14)
// vanilla JS imgLoad.on( 'always', function( instance ) { console.log('ALWAYS - all images have been loaded'); });, (*15)
Triggered after all images have been either loaded or confirmed broken. + `instance` _imagesLoaded_ - the imagesLoaded instance ### done ``` js // 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., (*16)
``` js $('#container').imagesLoaded().fail( function( instance ) { console.log('FAIL - all images loaded, at least one is broken'); });, (*17)
// vanilla JS imgLoad.on( 'fail', function( instance ) { console.log('FAIL - all images loaded, at least one is broken'); });, (*18)
Triggered after all images have been loaded with at least one broken image. ### progress ``` js 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., (*19)
instance
imagesLoaded - the imagesLoaded instanceimage
LoadingImage - the LoadingImage instance of the loaded imageImage - The img
element, (*20)
Boolean - true
when the image has succesfully loaded, (*21)
Array of LoadingImage instances for each image detected, (*22)
``` js 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 ); } });, (*23)
## Browserify imagesLoaded works with [Browserify](http://browserify.org/). ``` bash npm install imagesloaded --save
``` js var imagesLoaded = require('imagesloaded');, (*24)
imagesLoaded( elem, function() {...} );, (*25)
Use `.makeJQueryPlugin` to make to use `.imagesLoaded()` jQuery plugin. ``` js var $ = require('jquery'); var imagesLoaded = require('imagesloaded'); // provide jQuery argument imagesLoaded.makeJQueryPlugin( $ ); // now use .imagesLoaded() jQuery plugin $('#container').imagesLoaded( function() {...});
Install imagesLoaded and imports-loader with npm., (*26)
``` bash npm install imagesloaded imports-loader, (*27)
In your config file, `webpack.config.js`, use the imports loader to disable `define` and set window for `imagesloaded`. ``` js module.exports = { module: { loaders: [ { test: /imagesloaded|ev\-emitter/, loader: 'imports?define=>false&this=>window' } ] } };
(This hack is required because of an issue with how Webpack loads dependencies. +1 this issue on GitHub to help get this issue addressed.), (*28)
You can then require('imagesloaded')
., (*29)
``` js // main.js var imagesLoaded = require('imagesLoaded');, (*30)
imagesLoaded( '#container', function() { // images have loaded });, (*31)
Use `.makeJQueryPlugin` to make `.imagesLoaded()` jQuery plugin. ``` js // main.js var imagesLoaded = require('imagesLoaded'); var jQuery = require('jquery'); // provide jQuery argument imagesLoaded.makeJQueryPlugin( $ ); // now use .imagesLoaded() jQuery plugin $('#container').imagesLoaded( function() {...});
Run webpack., (*32)
``` bash webpack main.js bundle.js, (*33)
## RequireJS imagesLoaded works with [RequireJS](http://requirejs.org). You can require [imagesloaded.pkgd.js](http://imagesloaded.desandro.com/imagesloaded.pkgd.js). ``` js requirejs( [ 'path/to/imagesloaded.pkgd.js', ], function( imagesLoaded ) { imagesLoaded( '#container', function() { ... }); });
Use .makeJQueryPlugin
to make .imagesLoaded()
jQuery plugin., (*34)
``` js requirejs( [ 'jquery', 'path/to/imagesloaded.pkgd.js', ], function( $, imagesLoaded ) { // provide jQuery argument imagesLoaded.makeJQueryPlugin( $ ); // now use .imagesLoaded() jQuery plugin $('#container').imagesLoaded( function() {...}); });, (*35)
You can manage dependencies with [Bower](http://bower.io). Set `baseUrl` to `bower_components` and set a path config for all your application code. ``` js requirejs.config({ baseUrl: 'bower_components/', paths: { // path to your app app: '../' } }); requirejs( [ 'imagesloaded/imagesloaded', 'app/my-component.js' ], function( imagesLoaded, myComp ) { imagesLoaded( '#container', function() { ... }); });
Use imagesLoaded v3 for IE8 support., (*36)
imagesLoaded is released under the MIT License. Have at it., (*37)