Skip to content

Instantly share code, notes, and snippets.

@hayatbiralem
Last active November 26, 2015 15:32
Show Gist options
  • Save hayatbiralem/669bd9313aa2d9011aed to your computer and use it in GitHub Desktop.
Save hayatbiralem/669bd9313aa2d9011aed to your computer and use it in GitHub Desktop.
Images loaded function without imagesLoaded library
(function($){
var imagesLoaded = function(selector, onComplete, onProgress) {
var $images = $(selector).find('img');
var success = 0;
var error = 0;
var iteration = 0;
var total = $images.length;
var check = function(el, status) {
iteration++;
var data = {
img: el,
iteration: iteration,
success: success,
error: error,
total: total,
status: status
};
$.isFunction(onProgress) && onProgress(data);
if (success + error === total) {
$.isFunction(onComplete) && onComplete(data);
}
};
$images.each(function() {
this.onload = function() {
success++;
check(this, 'success');
};
this.onerror = function() {
error++;
check(this, 'error');
};
});
};
})(jQuery);
@hayatbiralem
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment