Skip to content

Instantly share code, notes, and snippets.

@onyxrev
Created June 11, 2014 01:59
Show Gist options
  • Save onyxrev/2555bf24dfb38a45d17c to your computer and use it in GitHub Desktop.
Save onyxrev/2555bf24dfb38a45d17c to your computer and use it in GitHub Desktop.
image size detector for use in determining the size of image that may or may not be loaded (in lieu of img.load)
// this imageSizeDetector works in lieu of img.onload
(function(jQuery){
jQuery.fn.imageSizeDetector = function(callback){
var $image = this;
var width = 0;
var height = 0;
var pollCount = 0;
var callback = callback;
var interval = setInterval(function(){
width = $image.outerWidth();
pollCount++;
// wait until the image is at least 40px wide
// ... or until we've polled for 10 seconds
if (width < 40 && pollCount < 200) return;
clearInterval(interval);
height = $image.outerHeight();
callback(width, height);
}, 50);
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment