How can I check if a background image is loaded?

后端 未结 10 1509
囚心锁ツ
囚心锁ツ 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:39

    try this:

    $('').attr('src', 'http://picture.de/image.png').on('load', function() {
       $(this).remove(); // prevent memory leaks as @benweet suggested
       $('body').css('background-image', 'url(http://picture.de/image.png)');
    });
    

    this will create new image in memory and use load event to detect when the src is loaded.

提交回复
热议问题