• Stars
    star
    8,878
  • Rank 4,095 (Top 0.09 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“· JavaScript is all like "You images done yet or what?"

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.

More Repositories

1

masonry

🏩 Cascading grid layout plugin
HTML
16,283
star
2

draggabilly

πŸ‘‡ Make that shiz draggable
JavaScript
3,856
star
3

3dtransforms

πŸ“¦ Introduction to CSS 3D transforms
CSS
1,011
star
4

classie

🎩 class helper functions
JavaScript
869
star
5

close-pixelate

Pixelate an image with <canvas> a la Chuck Close
JavaScript
725
star
6

colcade

Lightweight masonry layout
HTML
488
star
7

vanilla-masonry

[deprecated] Masonry layouts without any frameworks
JavaScript
327
star
8

practical-ui-physics

Learn basic physics for UI
JavaScript
324
star
9

breathing-halftone

Images go whoa with lots of floaty dots
JavaScript
254
star
10

windex

Pretty up your localhost. No more 1996 jank.
CSS
184
star
11

arpeggiator

Web audio arpeggiator
JavaScript
176
star
12

get-size

πŸ“ Measure elements
JavaScript
174
star
13

CSS3.tmbundle

TextMate bundle for new CSS3 properties. Includes vendor specific properties and HTML5 elements
108
star
14

jquery-bridget

πŸŒ‰ bridget makes jQuery plugins
JavaScript
108
star
15

zui-site-riot

Tutorial for zoom-scrolling on 2011.beercamp.com
JavaScript
54
star
16

eventie

event binding helper
JavaScript
54
star
17

doc-ready

lets get this party started... on document ready
JavaScript
49
star
18

sticky-titles

iOS/Android-like fixied titles that scroll sorta
JavaScript
33
star
19

jumbyl

Post to Tumblr from the command line
JavaScript
31
star
20

neo-vision

Chrome extension that syntax-highlights source code files with customizable themes
JavaScript
30
star
21

matches-selector

πŸ‘— matchesSelector helper
JavaScript
29
star
22

v3.desandro.com

All about me! In orange!
JavaScript
20
star
23

dotfiles

Personalized settings for Terminal, Git, TextMate, etc.
Shell
20
star
24

masonry-docs

πŸ“ Documentation for Masonry
Handlebars
20
star
25

textmate-bundles

My modifications to TextMate's bundles
HTML
19
star
26

v2.desandro.com

My personal site. Runs on Stacey CMS
PHP
18
star
27

curtis-css-typeface

type made with CSS shapes
CSS
18
star
28

transitn

JS utility class for CSS transitions
JavaScript
17
star
29

get-style-property

quick & dirty CSS property testing
JavaScript
16
star
30

welovemariokart

JavaScript
14
star
31

dropshado.ws

πŸ‘» blog about front-end development minutiae
JavaScript
13
star
32

smallblog

🐭 I don't know how to put this in words but this is me trying
CSS
9
star
33

ani-halftone

Animated halftones
JavaScript
9
star
34

typekit-table

Sort and filter high-quality fonts from Typekit
JavaScript
8
star
35

requirejs-bower-homework

Help me figure out this RequireJS business
JavaScript
7
star
36

jtetypes

Try out fonts by James T. Edmondson
JavaScript
7
star
37

lists

πŸ“œ of lists
7
star
38

motion-emotion

Presentation on CSS transitions and transforms
6
star
39

demo

Demos, tests, and misfit fun things
JavaScript
6
star
40

organize-bower-sources

Get Bower dependency source files, in order, listed by file extension.
JavaScript
5
star
41

nclud-v3-talk

Presentation outline for talk about nclud.com v3
JavaScript
5
star
42

issues-agreement

Help me dev you
5
star
43

touchy-feely-dev

Outline for presentation at Future Insights Live 2012
CSS
4
star
44

mcgilldesandro.com

I'm getting hitched. This is the website.
JavaScript
4
star
45

dotvim

Vim config
Vim Script
4
star
46

make-julesmarie-cry

A sadistic bookmarklet for "Click here" links
JavaScript
4
star
47

mobile-safari-machete

"Mobile Safari: Bring your machete" presentation from Front Trends 2012
JavaScript
4
star
48

prefs

Personal preferences
3
star
49

blahble

A boring Tumblr theme
HTML
3
star
50

open-source-aint-free

Talk for Valio Con 2012
JavaScript
3
star
51

concatminify.com

Help newbie devs learn concat & minify
CSS
3
star
52

bower-introduction

Notes for presentation
2
star
53

masonry-v2-3-shim

Maintain backwards compatibility using Masonry v3 with code for Masonry v2.
JavaScript
2
star
54

v4.desandro.com

Personal site
Handlebars
1
star
55

button-group

JavaScript
1
star