I'm writing some Javascript to resize the large image to fit into the user's browser window. (I don't control the size of the source images unfortunately.)

在HTML中会出现这样的内容:

<img id="photo"
     src="a_really_big_file.jpg"
     alt="this is some alt text"
     title="this is some title text" />

Is there a way for me to determine if the src image in an img tag has been downloaded?

I need this because I'm running into a problem if $(document).ready() is executed before the browser has loaded the image. $("#photo").width() and $("#photo").height() will return the size of the placeholder (the alt text). In my case this is something like 134 x 20.

现在我只是判断照片的高度是否小于150,并假设如果是,它只是alt文本.但这是一个相当大的漏洞,如果一张照片的高度低于150像素(在我的特殊情况下不太可能),或者如果alt文本的高度超过150像素(可能发生在一个小的浏览器窗口上),它就会崩溃.


Edit: For anyone wanting to see the code:

$(function()
{
  var REAL_WIDTH = $("#photo").width();
  var REAL_HEIGHT = $("#photo").height();

  $(window).resize(adjust_photo_size);
  adjust_photo_size();

  function adjust_photo_size()
  {
    if(REAL_HEIGHT < 150)
    {
      REAL_WIDTH = $("#photo").width();
      REAL_HEIGHT = $("#photo").height();
      if(REAL_HEIGHT < 150)
      {
        //image not loaded.. try again in a quarter-second
        setTimeout(adjust_photo_size, 250);
        return;
      }
    }

    var new_width = . . . ;
    var new_height = . . . ;

    $("#photo").width(Math.round(new_width));
    $("#photo").height(Math.round(new_height));
  }

});

Update: Thanks for the suggestions. There is a risk of the event not being fired if I set a callback for the $("#photo").load event, so I have defined an onLoad event directly on the image tag. For the record, here is the code I ended up going with:

<img id="photo"
     onload="photoLoaded();"
     src="a_really_big_file.jpg"
     alt="this is some alt text"
     title="this is some title text" />

Then in Javascript:

//This must be outside $() because it may get called first
var isPhotoLoaded = false;
function photoLoaded()
{
  isPhotoLoaded = true;
}

$(function()
{
  //Hides scrollbars, so we can resize properly.  Set with JS instead of
  //  CSS so that page doesn't break with JS disabled.
  $("body").css("overflow", "hidden");

  var REAL_WIDTH = -1;
  var REAL_HEIGHT = -1;

  $(window).resize(adjust_photo_size);
  adjust_photo_size();

  function adjust_photo_size()
  {
    if(!isPhotoLoaded)
    {
      //image not loaded.. try again in a quarter-second
      setTimeout(adjust_photo_size, 250);
      return;
    }
    else if(REAL_WIDTH < 0)
    {
      //first time in this function since photo loaded
      REAL_WIDTH = $("#photo").width();
      REAL_HEIGHT = $("#photo").height();
    }

    var new_width = . . . ;
    var new_height = . . . ;

    $("#photo").width(Math.round(new_width));
    $("#photo").height(Math.round(new_height));
  }

});

推荐答案

可以添加事件侦听器,也可以使用onLoad让映像自行通知.然后从那里算出尺寸.

<img id="photo"
     onload='loaded(this.id)'
     src="a_really_big_file.jpg"
     alt="this is some alt text"
     title="this is some title text" />

Jquery相关问答推荐

使用jquery将外部代码插入div

修改对象数组中的对象属性

javascript:无效(0); vs 返回 false vs preventDefault()

在 JavaScript 中解析 URL

如何在循环内创建动态变量名称?

SCRIPT7002:XMLHttpRequest:网络错误 0x2ef3,由于错误 00002ef3 无法完成操作

使用 bootstrap-datepicker 检测对选定日期的更改

使用跨域帖子发送凭据?

jQuery用另一个类替换一个类

Jquery .show() 不显示隐藏可见性的 div

将每 3 个 div 包装在一个 div 中

用 Javascript 加载 jQuery 并使用 jQuery

虽然未定义变量 - 等待

将片段添加到 URL 而不导致重定向?

检测用户是否创建了滚动事件

!!~(不是波浪号/bang bang 波浪号)如何改变包含/包含数组方法调用的结果?

删除除一个之外的所有类

你如何记录jQuery中一个元素触发的所有事件?

jQuery,简单的轮询示例

小于 10 给数字加 0