Last active
November 26, 2015 15:32
-
-
Save hayatbiralem/669bd9313aa2d9011aed to your computer and use it in GitHub Desktop.
Images loaded function without imagesLoaded library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From: desandro/imagesloaded#166
My Pen: http://codepen.io/hayatbiralem/pen/jbjLZe
Original Pen: http://codepen.io/desandro/pen/bIFyl