Dynamic Container Height (jQuery)

2019-06-08 11:39发布

问题:

I have a fixed-height (180px) header and fixed-height footer (50px). I'd like the container height to be: window height MINUS header MINUS footer.

How do I achieve this using jQuery?

Edited to Add: If the container height is updated on window resize, that'd be awesome!

回答1:

No need to use jquery.

With css (i marked the key points to get it working, you can change the values accordingly):

#header
{
    top: 0;
    left: 0;
    width: 100%;
    height: 80px;          /* KEY POINT */
    overflow: hidden;
}

#footer
{
    position: absolute;    /* KEY POINT */
    bottom: 0;
    left: 0;
    width: 100%;
    height: 36px;          /* KEY POINT */
    overflow: hidden;
}

#contents
{
    position: fixed;       /* KEY POINT */
    top: 83px;             /* KEY POINT */
    left: 0;
    right: 0;
    bottom: 39px;          /* KEY POINT */
    right: 0;
    overflow: auto;
}

Results in something like:

|----------------------------------------|
|              div#header                |
|            (80px height)               | 
|----------------------------------------|
|                                        |
|              div#contents              |
|         (top 83px, bottom 39px)        |
|                                        |
|----------------------------------------|
|              div#footer                |
|             (36px height)              |
|----------------------------------------|

This emulates old frames. In this example, there is a little span of 3px between each div.

EDIT2: If you use some other absolute positioned divs (like tooltips), you have to add this condition to avoid a strange flickering in IE7:

<!--[if IE 7]>
    <style type="text/css">
        #header
        {
            position: absolute;
        }
    </style>
<![endif]-->

EDIT3: seems like I didn't paste the whole thing. This bit is essential to get it working with IE6. Please note this is not one of the usual expression's hacks.

* html body
{
    /* YOU'LL RECOGNIZE THESE NUMBERS WITH THE EXAMPLE ABOVE */
    padding: 83px 0 39px 0;  
}

* html #contents
{
    height: 100%;
    width: 100%;
}

Read here for more infos.



标签: jquery window