How to verify background (css) image was loaded?

前端 未结 5 1400
离开以前
离开以前 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:37

    This article may help you. Relevant section:

    // Once the document is loaded, check to see if the
    // image has loaded.
    $(
        function(){
            var jImg = $( "img:first" );
    
            // Alert the image "complete" flag using the
            // attr() method as well as the DOM property.
            alert(
                "attr(): " +
                jImg.attr( "complete" ) + "\n\n" +
    
                ".complete: " +
                jImg[ 0 ].complete + "\n\n" +
    
                "getAttribute(): " +
                jImg[ 0 ].getAttribute( "complete" )
            );
        }
    );
    

    Basically select the background-image and do the check to see it's loaded.

提交回复
热议问题