This test loads images with position: absolute;, and when they are done loading, positions them next to each other based on their width.

Cloning test checks for premature callback execution in WebKit browsers. It it fails, images will be on top of each other in the upper left corner of their holder. This should never happen :) (You can click on cloned holders to remove them.)

Code


// Call imagesLoaded and position images
$holder.imagesLoaded(function( $images, $proper, $broken ){

    var $container = this,
        x = 1;

    $images.each( function() {
        var $this = $(this).css({ left: x });
        x += $this.width() + 1;
    });

    $container.width(x);

});
	
Total 0 / Proper 0 / Broken 0 / Deferred is pending


There is 10% chance for loading broken images.

As images are being loaded, .progress() method removes loading overlay from proper images, marks broken images as red, and updates progress bar to represent loading status.

Deferred object is rejected if at least one image in stack is broken.

Clicking on images removes the image and updates the stack info. (Remove broken images to test deferred resolution.)

Code


// Call imagesLoaded with multiple callbacks, and save the deferred object
var dfd = $holder.imagesLoaded({
        callback: function($images, $proper, $broken){

            $totalLabel.text( $images.length );
            $properLabel.text( $proper.length );
            $brokenLabel.text( $broken.length );

        },
        progress: function (isBroken, $images, $proper, $broken) {

            var loadingSpan = this.siblings('.loading');

            if( isBroken ){
                loadingSpan.removeClass('loading').addClass('broken');
            } else {
                loadingSpan.fadeOut(200, function(){ $(this).remove(); });
            }

            $progressBar.css({ width: Math.round( ( ( $proper.length + $broken.length ) * 100 ) / $images.length ) + '%' });

        }
    });

// Subsequent deferred method registration (not to be used with progress method)
dfd.always(function(){

    var dfdState = dfd.state();

    $dfdLabel.addClass( 'label-' + ( dfdState === 'resolved' ? 'success' : 'important' ) ).text( dfdState );

    $progress.hide();
    $statusBar.show();
    $progressBar.css({ width: 0 });

});
	
Fork me on GitHub