How can I check if a background image is loaded?

后端 未结 10 1377
囚心锁ツ
囚心锁ツ 2020-11-22 07:05

I want to set a background image on the body tag, then run some code - like this:

$(\'body\').css(\'background-image\',\'http://picture.de/image.png\').load(         


        
10条回答
  •  太阳男子
    2020-11-22 07:53

    Something like this:

    var $div = $('div'),
      bg = $div.css('background-image');
      if (bg) {
        var src = bg.replace(/(^url\()|(\)$|[\"\'])/g, ''),
          $img = $('').attr('src', src).on('load', function() {
            // do something, maybe:
            $div.fadeIn();
          });
      }
    });
    

提交回复
热议问题