我需要我的背景图像下我的菜单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>现在,两个警报都会随视口高度一起弹出。因此,什么都不会发生。
我使用的是Firefox 16.0.2
下面是指向实际页面http://servapps.dyndns-work.com/abstract/的链接
发布于 2013-04-29 14:02:24
这可以在CSS中完成。您需要确保所有包含元素的高度至少等于页面的高度(height: 100%)。这包括html和body元素。
这段代码应该适用于你的网站:
html, body, #bg {
height: 100%;
}发布于 2013-11-25 15:37:01
这可能失败的另一个原因是缺少文档类型声明(<!DOCTYPE html>)。添加此选项可修复$(window).height()以返回正确的视口高度。
https://stackoverflow.com/questions/16271950
复制相似问题