我需要我的背景图片位于我的菜单 div 下方。因此,我没有将背景应用于 body 元素,而是放入了另一个包含我的 body div 的 bg div,并将宽度设置为 100%。(我的 body div 具有指定的宽度)我在 bg div 中设置了背景。
我测试了一下,因为文档不够长,得到了半张背景图。所以我正在尝试为此做一个 javascript 修复。
<!DOCTYPE HTML>
.....
<script type="text/javascript" src="jquery-min.js"></script>
<script type="text/javascript">
function setDocumentSize() {
alert($(window).height());
alert($(document).height());
if ($(window).height()>$(document).height()) {
var height = $(window).height()-$(document).height();
document.getElementById('bg').style.height=height+"px"
}
}
.....
</script>
.....
<body onload="setDocumentSize()">
<div class="menu">
.....
</div>
<div class="bg">
<div class="body">
.....(background in this div)
</div>
</div>
现在这两个警报都会弹出视口(viewport)高度。因此什么也没有发生。
我使用的是 Firefox 16.0.2
最佳答案
失败的另一个原因是文档类型声明 (<!DOCTYPE html>
) 丢失。添加此修复 $(window).height()
返回正确的视口(viewport)高度。
关于javascript - jquery $(window).height() 和 $(document).height() 返回相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16271950/