jQuery or [removed] Determine when image finished loading

后端 未结 7 962
不思量自难忘°
不思量自难忘° 2020-12-05 10:15

How can I detect when an image has finished loading be it from the server or the browser cache? I want to load various images in the same tag and d

7条回答
  •  清歌不尽
    2020-12-05 11:12

    Everyone mentioned that the event should be fired before set to the src.

    But if you don't want to worry about it, you can use the oncomplete event (will be fired even with cached images) to fire onload, like this:

    $("img").one("load", function() {
      // do stuff
    }).each(function() {
      if(this.complete) $(this).load();
    });
    

    Using jQuery you don't have to worry about backward compatibility (eg: img.height>0 in IE)

提交回复
热议问题