How to create a JavaScript callback for knowing when an image is loaded?

前端 未结 10 1344
太阳男子
太阳男子 2020-11-22 04:56

I want to know when an image has finished loading. Is there a way to do it with a callback?

If not, is there a way to do it at all?

10条回答
  •  一整个雨季
    2020-11-22 05:52

    Here is jQuery equivalent:

    var $img = $('img');
    
    if ($img.length > 0 && !$img.get(0).complete) {
       $img.on('load', triggerAction);
    }
    
    function triggerAction() {
       alert('img has been loaded');
    }
    

提交回复
热议问题