I have an image on page that has been resized to fit in a div, say, 400x300. How can I get the full size of the image (~4000x3000) in jQuery? .width() and .height() only seem to return the current size of the image.
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to get the background from multiple images by
- How to fix IE ClearType + jQuery opacity problem i
You can do this with an
Image
object that holds the same source file like:Images have
naturalWidth
andnaturalHeight
properties that contain the actual, non-modified width and height of the image, i.e. the real dimensions of the image, not what CSS sets it to.One would still have to wait for the image to load though
Another option would be to create a new image with the same file as the source, and get the dimensions from that, as long as it's never added to the DOM, not external styles will affect it
FIDDLE
Try this:
var pic = $("img")
// need to remove these in of case img-element has set width and height pic.removeAttr("width"); pic.removeAttr("height");
var pic_real_width = pic.width(); var pic_real_height = pic.height();
You can clone the image, remove the height and width attributes, append it to the body and get the width and size before removing it.
jsFiddle demo is here: http://jsfiddle.net/58dA2/
Code is: