jQuery event for images loaded

后端 未结 14 1199
离开以前
离开以前 2020-11-22 15:25

Is it possible to detect when all images are loaded via a jQuery event?

Ideally, there should be a

$(document).idle(function()
{
}

相关标签:
14条回答
  • 2020-11-22 15:58

    If you want to check for not all images, but a specific one (eg. an image that you replaced dynamically after DOM is already complete) you can use this:

    $('#myImage').attr('src', 'image.jpg').on("load", function() {  
      alert('Image Loaded');  
    });  
    
    0 讨论(0)
  • 2020-11-22 16:00

    Use of the jQuery $().load() as an IMG event handler isn't guaranteed. If the image loads from the cache, some browsers may not fire off the event. In the case of (older?) versions of Safari, if you changed the SRC property of an IMG element to the same value, the onload event will NOT fire.

    It appears that this is recognized in the latest jQuery (1.4.x) - http://api.jquery.com/load-event - to quote:

    It is possible that the load event will not be triggered if the image is loaded from the browser cache. To account for this possibility, we can use a special load event that fires immediately if the image is ready. event.special.load is currently available as a plugin.

    There is a plug-in now to recognize this case and IE's "complete" property for IMG element load states: http://github.com/peol/jquery.imgloaded/raw/master/ahpi.imgload.js

    0 讨论(0)
提交回复
热议问题