我有以下JQuery,每当调整窗口大小或首次加载时都会调用它。我有一些问题,它似乎在第一次加载时无法按预期工作。有人可以告诉我什么是故障吗?
JQuery (1.8.3)
function setContentHeight() {
var height = $(document).height();
$('#content, #content > div:first-child').height(height - 242);
$('#content-text-description').height(height - 280);
}
$(window).resize(function(e) {
setContentHeight();
});
$(document).ready(function(e) {
setContentHeight();
});
.HTML
<body>
<div id="container">
<div id="banner"></div> <!-- Header -->
<div id="navi"></div> <!-- Navigation Pane -->
<div id="content"> <!-- Content loaded with include -->
<div id="home"> <!-- Start of content -->
<div id="content-text-description"></div> <!-- container for custom scroller -->
</div>
<div id="footer"></div> <!-- Footer -->
</div>
</body>
.CSS
body {
margin:0;
font-family:"Century Gothic";
line-height:30px;
}
#container {
width:1024px;
margin: 0 auto;
}
#banner {
width:1024px;
}
#content {
width:1024px;
height:470px;
z-index:98;
float:left;
top:-7px;
overflow:hidden;
}
#footer {
text-align:center;
background:#59585D;
position:absolute;
bottom:0px;
width:1024px;
height:30px;
font-size:12px;
color:#ffffff;
}
#content-text-description {
padding-top:20px;
display:block;
width:800px;
z-index:3;
overflow:auto;
}
据我所知,JQuery 位应该通过在加载文档或调整窗口大小时调整请求的div 大小来按预期运行,但事实并非如此。
是我做错了什么,还是我试图实现的目标是不可能的?
我通过更多地玩代码来修复它......
真的很简单。
var height = $(document).height();
应更改为:
var height = $(window).height();
Cdocument.ready 事件为时过早,无法检查高度,请改用 document.load 事件
$(window).resize(function(e) {设置内容高度() ;});
$(document).load(function(e) {设置内容高度() ;});
原因是 document.load 事件在下载整个 dom 后不久被触发。但是在那个tme渲染没有发生,css可能不会应用,图像没有加载,等等。因此,高度将不正确。
但是加载事件在下载完所有内容并应用样式后触发,因此您应该获得正确的高度。
无论如何,这不是你应该用脚本做的事情,而是用样式来做。