jQuery callback on image load (even when the image is cached)

前端 未结 14 1168
轻奢々
轻奢々 2020-11-21 06:25

I want to do:

$(\"img\").bind(\'load\', function() {
  // do stuff
});

But the load event doesn\'t fire when the image is loaded from cache

14条回答
  •  北海茫月
    2020-11-21 07:28

    A modification to GUS's example:

    $(document).ready(function() {
        var tmpImg = new Image() ;
        tmpImg.onload = function() {
            // Run onload code.
        } ;
    
    tmpImg.src = $('#img').attr('src');
    })
    

    Set the source before and after the onload.

提交回复
热议问题