Trigger event on background image load

后端 未结 4 1466
甜味超标
甜味超标 2021-01-11 11:56

Is there a way, using jQuery, to trigger an event on load of a CSS background image?

4条回答
  •  广开言路
    2021-01-11 12:15

    $('').attr('src',function(){
        var imgUrl = $('#body').css('background-image');
        imgUrl = imgUrl .substring(5, imgUrl .length-2);
        return imgUrl;
    }).load(function(){
        //Do Something
    });
    

    Could not help but notice that the above code does not work in fiddle. Looks like its because it returns "url('/img-path.jpg')" instead of just "/img-path.jpg".

    However this method still fails in IE. Anyone have an ie fix?

提交回复
热议问题