Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save snowman-repos/8840283 to your computer and use it in GitHub Desktop.
Save snowman-repos/8840283 to your computer and use it in GitHub Desktop.
Determining Max-Width of Responsive Images
# 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