【问题标题】:Getting aspect ratio fo image获取图像的纵横比
【发布时间】:2014-10-30 05:26:15
【问题描述】:

我正在将图像附加到这样的 div:

$thisComp.find('.DummyImage').attr("src", this.imageUrl);

在此之后的行中,我正在使用 ajax 调用并获取图像的纵横比,如下所示:

$.ajax(this.imageUrl).done(function () { var AspectRatio = ($thisComp.find('.DummyImage').width() / $thisComp.find('.DummyImage').height()).toFixed(2);

在这种情况下,我有时会得到正确的纵横比值,有时在其他浏览器中得到 NaN(特别是在 Chrome 浏览器中),它总是可以正常工作。 我认为问题在于 ajax 调用在它上面的行完全加载“src”图像之前首先被执行。

请针对此问题提出可能的解决方案。

【问题讨论】:

    标签: javascript jquery ajax


    【解决方案1】:

    等待图像的加载事件,而不是使用$.ajax 加载图像。这是 2 个单独的请求!

    var $img = $thisComp.find('.DummyImage');
    .on("load", function() {
        alert($img.width() / $img.height());
    })
    .attr("src", this.imageUrl);
    

    【讨论】:

      猜你喜欢
      • 2018-10-25
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多