Last active
August 29, 2015 14:08
-
-
Save kalepail/12821f09cf354fef466e to your computer and use it in GitHub Desktop.
Get an image's natural width and height
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 props = ['Width', 'Height'], prop; | |
while (prop = props.pop()) { | |
(function (natural, prop) { | |
$.fn[natural] = (natural in new Image()) ? | |
function () { | |
return this[0][natural]; | |
} : | |
function () { | |
var | |
node = this[0], | |
img, | |
value; | |
if (node.tagName.toLowerCase() === 'img') { | |
img = new Image(); | |
img.src = node.src, | |
value = img[prop]; | |
} | |
return value; | |
} | |
}('natural' + prop, prop.toLowerCase())); | |
} | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment