获取已调整大小的html图像元素的原始尺寸

dev*_*os1 5 javascript html5 image

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

ant*_*rat 22

存在naturalWidthnaturalHeightDOM属性.

例如:

var image = new Image();
image.src = "test.jpg";
image.onload = function() {
    alert('width - ' + image.naturalWidth);
    alert('height - ' + image.naturalHeight);
}
Run Code Online (Sandbox Code Playgroud)

或见例如上的jsfiddle.

更多信息在MDN