How to verify background (css) image was loaded?

前端 未结 5 1401
离开以前
离开以前 2021-02-03 22:00

I have the following CSS class that I\'m applying on a tag:

.bg {
   background-image: url(\'bg.jp         


        
5条回答
  •  终归单人心
    2021-02-03 22:43

    The only way I know of to do this is to load the image using Javascript, and then set that image as the backgroud.

    For example:

    var bgImg = new Image();
    bgImg.onload = function(){
       myDiv.style.backgroundImage = 'url(' + bgImg.src + ')';
    };
    bgImg.src = imageLocation;
    

提交回复
热议问题