【问题标题】:how to get an image actual width and height [duplicate]如何获得图像的实际宽度和高度[重复]
【发布时间】:2012-11-23 15:40:00
【问题描述】:

可能重复:
Get real image width and height with JavaScript in Safari/Chrome?

当图像在 css 中按比例缩小时,有没有办法使用 jQuery 知道图像的真实宽度和高度 - 没有任何额外的尺寸数据 - 任何链接或代码示例都会有所帮助,谢谢

【问题讨论】:

标签: javascript jquery


【解决方案1】:

试试这样的:

$("<img/>").attr("src", imgSrc).load(function() {
        pic_real_width = this.width;   
        pic_real_height = this.height;          
    });

问题是,您只能在加载图像时访问图像的真实宽度和高度,您也可以在没有任何容器的情况下将图像加载到您看不到的地方并要求 ("img").css ('width'),但我认为这是一种更优雅的方式。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    尝试使用naturalWidthnaturalHeight

    var domElement = $('yourImage')[0]; // or document.getElementById('yourImageId');
    domElement.naturalWidth
    domElement.naturalHeight
    

    【讨论】:

    猜你喜欢
    • 2011-11-26
    • 1970-01-01
    • 2015-05-05
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 2013-09-24
    相关资源
    最近更新 更多