【问题标题】:find Image correct width and height找到图像正确的宽度和高度
【发布时间】:2011-03-01 12:47:05
【问题描述】:

现在我在 img 的 onload 函数时获得图像的宽度和高度。我的问题是,图像原始宽度 = 500px 但 document.getElementId(id).offsetWidth 只给出 300px 和高度。请帮助我如何获得图像的原始宽度和高度

【问题讨论】:

    标签: javascript


    【解决方案1】:

    至少 Gecko(Firefox)和基于 WebKit(Safari、Chrome)的浏览器应该为图像元素实现属性 naturalWidthnaturalHeight

    var image = document.getElementById("imageid");
    if (image.naturalHeight === undefined) {
      alert("Your browser doesn't support this example!");
    } else {
      var message = "The original size of the image:";
      message += "\n height: " + image.naturalHeight + "px";
      message += "\n width: " + image.naturalWidth + "px";
      alert(message);
    

    }

    【讨论】:

    • @c0mrade 因为如果图像已缩放,则 image.width 和 image.height 属性返回缩放后的大小,而不是原始大小。至少在 Firefox 中。
    【解决方案2】:

    对不起,我的浏览器不支持 naturalHeight 和 naturalWidth..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多