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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:37:15  来源:igfitidea点击:

Get original dimensions of resized html image element

javascriptimagehtml

提问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-heightor max-width)?

是否有一种简单有效的方法来获取显示在<img>具有潜在不同渲染大小(例如,通过max-heightmax-width)的元素中的图像的真实尺寸(在 JavaScript 中)?

回答by antyrat

There is present naturalWidthand naturalHeightDOM attributes.

有 presentnaturalWidthnaturalHeightDOM 属性。

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