【发布时间】:2011-12-29 04:17:12
【问题描述】:
我遇到了与此海报相同的问题: Jquery problem with height() and resize()
但该解决方案无法解决我的问题。我有三个堆叠的 div,我想使用 JQuery 使中间的高度调整为窗口高度的 100%,减去其他顶部和底部 div 的高度 (23px * 2)。它适用于调整大小,但在文档最初加载时它会关闭(短)16px。
HTML
<body>
<div id="bg1" class="bg">top</div>
<div id="content">
help me. seriously.
</div>
<div id="bg2" class="bg">bottom</div>
</body>
CSS
html,
body {
width:100%;
height:100%;
}
.bg {
width:315px;
height:23px;
margin:0 auto;
text-indent:-9000px;
}
#bg1 {background:url(../images/bg1.png) 0 50% no-repeat;}
#bg2 {background:url(../images/bg2.png) 0 50% no-repeat;}
#content {
width:450px;
margin:0 auto;
text-align: center;
}
jQuery
$(document).ready(function(){
resizeContent();
$(window).resize(function() {
resizeContent();
});
});
function resizeContent() {
$height = $(window).height() - 46;
$('body div#content').height($height);
}
【问题讨论】:
-
我在寻找这样的东西吗?还是页脚应该随页面移动? cssplay.co.uk/layouts/basics2.html 提琴就好了!
-
我可以做类似的事情,但我坚持使用将在服务器端生成的 HTML。我被要求用 JQuery 解决这类问题。
-
9000px 文本缩进是怎么回事?
-
参见“使用文本缩进隐藏文本”:reference.sitepoint.com/css/text-indent