用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 答案,谢谢! :)
最佳答案
offsetHeight
和 offsetWidth
将为您提供所需的内容,并考虑 maxWidth 和 maxHeight。请记住,img 对象需要成为 DOM 的一部分才能具有任何尺寸。因此,在 offsetHeight 和 width 返回任何有用的内容之前,您需要添加类似 document.body.appendChild($new_image)
的内容。
关于javascript - 设置 maxHeight 和 maxWidth 和 auto 后获取高度和宽度 - 纯 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8800272/