Created
June 11, 2014 01:59
-
-
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 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
// 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