Created
February 6, 2014 08:29
-
-
Save snowman-repos/8840283 to your computer and use it in GitHub Desktop.
Determining Max-Width of Responsive Images
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 | |
getMaxWidth = (img) -> | |
maxWidth = undefined | |
# Check if naturalWidth is supported | |
if img.naturalWidth isnt `undefined` | |
maxWidth = img.naturalWidth | |
# Not supported, use in-memory solution as fallback | |
else | |
image = new Image() | |
image.src = img.src | |
maxWidth = image.width | |
# Return the max-width | |
maxWidth | |
# image has to be fully loaded before you can check its dimensions! | |
hasDimensions = (img) -> | |
!!((img.complete and typeof img.naturalWidth isnt "undefined") or img.width) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment