2010-06-09 88 views
0

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

回答

0

至少Gecko-(火狐)和基于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); 

}

+0

为什么不只是image.height? – ant 2010-06-09 07:50:59

+0

@ c0mrade因为如果图像已经缩放,image.width和image.height属性会返回缩放的大小,而不是原始大小。至少在Firefox中。 – kkyy 2010-06-09 08:07:48

+0

好的tnx的信息 – ant 2010-06-09 08:30:48

0

对不起我的浏览器不支持naturalHeight和naturalWidth ..