JQUERY:将div调整为窗口宽度 [英] JQUERY: Resize div to window width
问题描述
我试图将div的宽度强制为浏览器窗口的宽度.这样,即使调整了窗口的大小,它的宽度也可以调整.
I am trying to force the width of a div to the width of a browser window. So that even if the window is resize the adjusts it's width.
我已经研究了大约一个小时,发现了诸如.resize()之类的零碎零碎的东西,但找不到任何有效的方法.
I have been researching for about an hour and have found odd bits and pieces such as .resize() but can't find anything which works.
推荐答案
默认情况下,没有容器来限制宽度,div应该跨越浏览器窗口的宽度.您可以将宽度明确设置为100%,但这不是必需的:
Without a container limiting the width, a div should span the width of the browser window by default. You can explicitly set the width to 100%, but that shouldn't be necessary:
<div style="width:100%;">Hello world</div>
我认为CSS在这里更合适,但是您可以像这样在jQuery中做到这一点:
I think CSS is more appropriate here, but you can do this in jQuery like this:
$("#div1").width($(window).width());
要在调整窗口大小时运行上述代码,您可以执行以下操作:
To run the above code whenever the window is resized, you can do this:
$(window).resize(function(){
$("#div1").width($(window).width());
});
这是 jsFiddle .为了演示起见,div在单击按钮时展开.
Here's a jsFiddle. For the sake of demonstration, the div expands on button click.
这篇关于JQUERY:将div调整为窗口宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!