Check if an image is loaded (no errors) with jQuery

前端 未结 15 1690
挽巷
挽巷 2020-11-21 20:45

I\'m using JavaScript with the jQuery library to manipulate image thumbnails contained in a unordered list. When the image is loaded it does one thing, when an error occurs

15条回答
  •  天命终不由人
    2020-11-21 21:26

    var isImgLoaded = function(imgSelector){
      return $(imgSelector).prop("complete") && $(imgSelector).prop("naturalWidth") !== 0;
    }
    

    // Or As a Plugin

        $.fn.extend({
          isLoaded: function(){
            return this.prop("complete") && this.prop("naturalWidth") !== 0;
          }
        })
    
    // $(".myImage").isLoaded() 
    

提交回复
热议问题