javascript - 设置 maxHeight 和 maxWidth 和 auto 后获取高度和宽度 - 纯 javascript

标签 javascript height width

用javascript设置maxHeight和maxWidth后是否可以获取图像的宽度和高度。

$new_image = new Image();
$new_image.src = 'xxx.jpg';
$new_image.style.maxHeight = '500px';
$new_image.style.maxWidth = '600px';
$new_image.style.height = 'auto';
$new_image.style.width = 'auto';
alert('CURRENT WIDTH AND HEIGHT IF IMAGE IS 1000 X 1000 PX');

我知道如何获取 maxheight 值,但这可能不是实际大小,因为图像设置为“自动”高度和宽度。

没有 jquery 答案,谢谢! :)

最佳答案

offsetHeightoffsetWidth 将为您提供所需的内容,并考虑 maxWidth 和 maxHeight。请记住,img 对象需要成为 DOM 的一部分才能具有任何尺寸。因此,在 offsetHeight 和 width 返回任何有用的内容之前,您需要添加类似 document.body.appendChild($new_image) 的内容。

关于javascript - 设置 maxHeight 和 maxWidth 和 auto 后获取高度和宽度 - 纯 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8800272/

相关文章:

html - css自动高度div

java - 如何计算男性和女性的理想体重范围

jquery - 当宽度设置为自动时,对话框的最大宽度不受影响

xcode - ScrollView 内的 Swift View 增加下拉刷新时的宽度

javascript - 当屏幕变宽时,视频不会全宽

javascript - Jquery pulsate 完成后可以做一些事情吗?

JavaScript 比较 2 个数组(某种意义上)

javascript - 保存并再次显示查看用户选择

javascript - jQuery 在脚本中创建类

css - 在 div 中“拉伸(stretch)”背景图像 - 100% 高度