Get the Size of a CSS Background Image Using JavaScript?

前端 未结 5 1191
时光说笑
时光说笑 2020-11-30 04:31

Is it possible to use JavaScript to get the actual size (width and height in pixels) of a CSS referenced background image?

5条回答
  •  被撕碎了的回忆
    2020-11-30 05:22

    var actualImage = new Image();
    actualImage.src = $('YOUR SELECTOR HERE').css('background-image').replace(/"/g,"").replace(/url\(|\)$/ig, "");
    
    actualImage.onload = function() {
        width = this.width;
        height = this.height;
    }
    

提交回复
热议问题