如何使用JavaScript获取图像大小(高度和宽度)? [英] How to get image size (height & width) using JavaScript?

查看:100
本文介绍了如何使用JavaScript获取图像大小(高度和宽度)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何jQuery或纯JS API或方法来获取页面上图像的尺寸?

Are there any jQuery or pure JS APIs or methods to get the dimensions of an image on the page?

推荐答案

< a href =https://developer.mozilla.org/en/DOM/element.clientWidth =noreferrer> 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;

这篇关于如何使用JavaScript获取图像大小(高度和宽度)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆