Created
January 27, 2014 10:14
-
-
Save jjmu15/8646161 to your computer and use it in GitHub Desktop.
Get image max width - This will give us image’s max-width: 100% value in pixels and it’s supported in IE9, Chrome, Firefox, Safari and Opera. We can also take this further and add support for older browsers by loading the image into an in-memory object:
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
// Get image's max-width:100%; in pixels | |
function getMaxWidth(img) { | |
var maxWidth; | |
// Check if naturalWidth is supported | |
if (img.naturalWidth !== undefined) { | |
maxWidth = img.naturalWidth; | |
// Not supported, use in-memory solution as fallback | |
} else { | |
var image = new Image(); | |
image.src = img.src; | |
maxWidth = image.width; | |
} | |
// Return the max-width | |
return maxWidth; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment