admin管理员组文章数量:1390604
I need my background image to be below my menu div. So instead of applying the background to the body element I put in another bg div that contains my body div and set the width to 100%.(My body div has a specified width) I set the background inside the bg div.
I tested it and I got half a background picture because the document was not long enough. So I'm attempting to make a javascript fix for this.
<!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>
Now both alerts pop up with the viewport height. Therefore nothing happens.
I'm using Firefox 16.0.2
Here is a link to the actual page /
I need my background image to be below my menu div. So instead of applying the background to the body element I put in another bg div that contains my body div and set the width to 100%.(My body div has a specified width) I set the background inside the bg div.
I tested it and I got half a background picture because the document was not long enough. So I'm attempting to make a javascript fix for this.
<!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>
Now both alerts pop up with the viewport height. Therefore nothing happens.
I'm using Firefox 16.0.2
Here is a link to the actual page http://servapps.dyndns-work./abstract/
Share Improve this question asked Apr 29, 2013 at 5:48 serv92serv92 1212 silver badges10 bronze badges 2- 1 When your doc is not beyond the window (no scrollbar) They show the same. When you minimize your window to the point the scrollbar pops up and refresh your page, you may get the clue .. :-) – Daniel Commented Apr 29, 2013 at 5:51
- why not just use a min-height of the image height for the background div? – VictorKilo Commented Apr 29, 2013 at 5:51
2 Answers
Reset to default 5One more reason this could fail is when the doctype declaration (<!DOCTYPE html>
) is missing. Adding this fixes the $(window).height()
to return proper viewport height.
This can be done in CSS. You need to make sure that all containing elements are at least the height of the page (height: 100%
). This includes both the html
and body
elements.
This code should work with your website:
html, body, #bg {
height: 100%;
}
本文标签: javascriptjquery (window)height() and (document)height() return the sameStack Overflow
版权声明:本文标题:javascript - jquery $(window).height() and $(document).height() return the same - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744740876a2622598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论