Get the size of background-image with jQuery after containing with CSS

前端 未结 6 1040
渐次进展
渐次进展 2021-01-22 00:16
#img {
    width:300px;
    height:300px;
    overflow:hidden;
    background-repeat: no-repeat !important;
    background-position: center !important;
    background-si         


        
6条回答
  •  遇见更好的自我
    2021-01-22 00:42

    window.onload = function() {
    
        var imageSrc = document
                        .getElementById('hello')
                         .style
                          .backgroundImage
                           .replace(/url\((['"])?(.*?)\1\)/gi, '$2')
                            .split(',')[0];
    
        // I just broke it up on newlines for readability        
    
        var image = new Image();
        image.src = imageSrc;
    
        var width = image.width,
            height = image.height;
    
        alert('width =' + width + ', height = ' + height)  
    
    }
    

提交回复
热议问题