获取背景图像宽度 [英] Obtain background image width
本文介绍了获取背景图像宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有可能获得在CSS中定义的背景图片宽度吗?
is possible to get background image width which is defined in CSS?
body {
background-image: url('background.jpg');
}
我需要这样的:
window.getComputedStyle(document.body, 'background-width');
推荐答案
var u = window.getComputedStyle(document.body, 'background-image');
u = u.replace(/url\(/, '').replace(/\)/, '');
var w = 0;
var i = new Image();
i.onload = function( ){ w = this.width; alert(w); }
i.src = u;
警告:我已经省略了一个案例,当body没有指定背景图片时,保留
这篇关于获取背景图像宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文