找到图像正确的宽度和高度

时间:2010-06-09 07:17:45

标签: javascript

现在我在img的onload函数时得到图像的宽度和高度。我的问题是,图像原始宽度= 500px但document.getElementId(id).offsetWidth只给出300px和高度。请帮助我如何获得图像的原始宽度和高度

2 个答案:

答案 0 :(得分:0)

至少Gecko-(Firefox)和基于WebKit(Safari,Chrome)的浏览器应该为图像元素实现属性naturalWidthnaturalHeight

var image = document.getElementById("imageid");
if (image.naturalHeight === undefined) {
  alert("Your browser doesn't support this example!");
} else {
  var message = "The original size of the image:";
  message += "\n height: " + image.naturalHeight + "px";
  message += "\n width: " + image.naturalWidth + "px";
  alert(message);

}

答案 1 :(得分:0)

抱歉,我的浏览器不支持naturalHeight和naturalWidth ..

相关问题