Get the Size of a CSS Background Image Using JavaScript?

前端 未结 5 1181
时光说笑
时光说笑 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:11

    var dimension, image;
    
    image = new Image();
    image.src = {url/data}
    image.onload = function() {
        dimension = {
            width: image.naturalWidth,
            height: image.naturalHeight
        };
        console.log(dimension); // Actual image dimension
    };
    

提交回复
热议问题