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
Add a ment  | 

2 Answers 2

Reset to default 5

One 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