【问题标题】:Get original dimensions of resized html image element获取调整大小的html图像元素的原始尺寸
【发布时间】:2012-03-16 01:38:02
【问题描述】:

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

【问题讨论】:

    标签: javascript image html


    【解决方案1】:

    存在naturalWidthnaturalHeight DOM 属性。

    例如:

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

    或者在 jsFiddle 上查看 example

    更多信息MDN

    【讨论】:

    • 太棒了!这正是我想要的。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 2023-03-27
    • 1970-01-01
    • 2020-04-19
    • 2012-12-16
    • 2023-02-04
    • 1970-01-01
    相关资源
    最近更新 更多