+0
日增长
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( 'conTop issues
#156Works fine locally but fires early on production26 comments · 2014-07-04#306Imagesloaded v5 triggers loading of largest image when using srcset7 comments · 2022-09-23#136Does imagesLOaded not fire when the src attribute of an img element is changed with javascript?6 comments · 2014-03-11#270Background load is not detected. Always calls the 'isLoaded' callback.5 comments · 2017-12-18#197Images Loaded with HTML5 Video Poster Attribute5 comments · 2015-06-17#66add timeout option5 comments · 2013-05-27#277Add support for SVG and <image> tag4 comments · 2018-08-06#308obj is not iterable3 comments · 2022-10-19
Latest release
v4.1.4 v4.1.4 2018-01-02Check for non-zero naturalWidth; Fixes bug with metafizzy/infinite-scroll671