javascript 获取调整大小的 html 图像元素的原始尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9725024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get original dimensions of resized html image element
提问by devios1
Is there a simple and efficient way to get the true dimensions (in JavaScript) of an image that is displayed in an <img>
element with a potentially different rendered size (e.g. via max-height
or max-width
)?
是否有一种简单有效的方法来获取显示在<img>
具有潜在不同渲染大小(例如,通过max-height
或max-width
)的元素中的图像的真实尺寸(在 JavaScript 中)?
回答by antyrat
There is present naturalWidth
and naturalHeight
DOM attributes.
有 presentnaturalWidth
和naturalHeight
DOM 属性。
For example:
例如:
var image = new Image();
image.src = "test.jpg";
image.onload = function() {
alert('width - ' + image.naturalWidth);
alert('height - ' + image.naturalHeight);
}
Or see exampleon jsFiddle.
或见例如上的jsfiddle。
More info at MDN
更多信息请访问MDN