如何使用jQuery设置高度和宽度的背景大小

时间:2014-01-15 06:49:57

标签: jquery

我正在尝试使用jQuery设置di​​v的background-size(它应该依赖于其他元素)。问题是,虽然我可以用一个值设置它,如下所示:

contentHeight = $('#content').outerHeight(true);
$('#background').css('background-size', contentHeight);

如果我这样做,它什么都不做:

contentHeight = $('#content').outerHeight(true);
$('#background').css('background-size', 'auto ' + contentHeight + 'px !important');

我有一种感觉,我犯了一个愚蠢的错误,但我没有看到它......

2 个答案:

答案 0 :(得分:2)

你需要这样做:

contentHeight = $('#content').outerHeight(true);
$('#background').css('background-size', "auto " + contentHeight + "px", "important");

或者

contentHeight = $('#content').outerHeight(true);
$('#background').css({'background-size': "auto " + contentHeight + "px !important"});

答案 1 :(得分:1)

见: -

DEMO

var size = "auto " + contentHeight + "px !important";
alert(size);
$('#background').css('background-size', size);
相关问题