[英]jquery $(window).height() and $(document).height() return the same
我需要背景圖片位於菜單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
可能失敗的另一個原因是缺少doctype聲明( <!DOCTYPE html>
)。 添加此選項可修復$(window).height()
以返回正確的視口高度。
這可以在CSS中完成。 您需要確保所有包含的元素至少是頁面的高度( height: 100%
)。 這包括html
和body
元素。
此代碼應與您的網站一起使用:
html, body, #bg {
height: 100%;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.