Voilà is a jQuery plugin that provides callbacks for images, letting you know when they've loaded.
It's based on the work done by David DeSandro on imagesLoaded
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="voila.js"></script>
new Voila(element, callback);
element
Element, NodeList, Array, or Selector Stringcallback
Function - function called when all images have been loaded
Additional callbacks are available by providing options instead of the callback:
new Voila('#container', {
complete: function(instance) {
console.log('COMPLETE - All images have finished loading');
},
progress: function(instance, image) {
var status = image.loaded ? 'loaded' : 'broken';
console.log('PROGRESS - Image ' + status + ': ' + image.img.src);
},
error: function(instance) {
console.log('ERROR - All images finished loading, but some are broken');
},
success: function(instance) {
console.log('SUCCESS - All images finished loading succesfully');
}
});
A voila
instance can be stored, exposing some extra properties and functions:
var voila = new Voila('#container', callback);
voila.images
Array - contains animage
object for eachimg
element foundvoila.abort()
Function - aborts all callbacks
Within the callbacks the voila
instance is always the first argument, the second one can be an image
object.
image.img
ImageElement - theimg
elementimage.loaded
Boolean -true
when succesfully loaded
Here's how to find out which images have succesfully loaded within the complete callback:
new Voila('#container', function(instance) {
$.each(instance.images, function(i, image) {
var status = image.loaded ? 'loaded' : 'broken';
console.log(status + ': ' + image.img.src);
});
});
Some sugar is added to jQuery for convenience:
var voila = $('#container').voila(callback);