且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用JavaScript获取图像大小(高度和宽度)?

更新时间:2023-01-15 12:31:25

clientWidth 和 clientHeight 是DOM属性,显示DOM元素内部维度的当前浏览器内大小(不包括边距和边框)。因此,对于IMG元素,这将获得可见图像的实际尺寸。

clientWidth and clientHeight are DOM properties that show the current in-browser size of the inner dimensions of a DOM element (excluding margin and border). So in the case of an IMG element, this will get the actual dimensions of the visible image.

var img = document.getElementById('imageid'); 
//or however you get a handle to the IMG
var width = img.clientWidth;
var height = img.clientHeight;