获取已调整大小的html图像元素的原始尺寸 [英] Get original dimensions of resized html image element

查看:216
本文介绍了获取已调整大小的html图像元素的原始尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单有效的方法来获取图像的真实尺寸(在JavaScript中),该图像显示在< img> 元素中,并且可能具有不同的渲染效果大小(例如通过 max-height max-width )?

Is there a simple and efficient way to get the true dimensions (in JavaScript) of an image that is displayed in an <img> element with a potentially different rendered size (e.g. via max-height or max-width)?

推荐答案

存在 naturalWidth naturalHeight DOM属性。

例如:

var image = new Image();
image.src = "test.jpg";
image.onload = function() {
    alert('width - ' + image.naturalWidth);
    alert('height - ' + image.naturalHeight);
}

或者参见示例

MDN

这篇关于获取已调整大小的html图像元素的原始尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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