3

Given a div on a page like <div id="rightRail>xxx</div>

Is it possible to some how magically make that div's height resize on broswer load/resize so that the height reaches the bottom of the window? It would be easier if the div was at the top of the page, but it isnt. thanks

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

3 Answers3

17

You need the .offset()help top value and the innerHeight from the current window to set its .height()help. This would look like:

$('#div').height(function(index, height) {
    return window.innerHeight - $(this).offset().top;
});

Demo: http://jsfiddle.net/d3ays/

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • If the page is taller than the browser window this will end up setting the footer height to 0. It also doesn't take into account any padding that may be applied to the div. Here is code that compares the heights as well as adjusts for padding. `$('#div').height(function(index, height) { var current_height = $(this).height(); var new_height = window.innerHeight - $(this).offset().top - parseInt($(this).css('padding-top')) - parseInt($(this).css('padding-bottom')); if (new_height > current_height) return new_height; }); ` – todd Nov 06 '13 at 21:34
1

If the page is taller than the browser window jAndy's solution will end up setting the div height to 0. It also doesn't take into account any padding that may be applied to the div. Here is code that compares the heights as well as adjusts for padding.

$('#div').height(function(index, height) {
    var current_height = $(this).height();
    var new_height = window.innerHeight - $(this).offset().top - parseInt($(this).css('padding-top')) - parseInt($(this).css('padding-bottom'));
    if (new_height > current_height) return new_height;
});
todd
  • 355
  • 3
  • 11
0

You can try the following code where this function is used for getting the height of window:

function GetHeight()
{
    var y = 0;
    if (self.innerHeight)
    {
            y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
            y = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
            y = document.body.clientHeight;
    }
    return y;
}

And then use jQuery to give height to your div dynamically like this:

 document.ready(function(){
  var heightOfDiv = GetHeight();
  jQuery('#rightRail').css("top",heightOfDiv +"px");
});

After you can manipulate the variable heightOfDiv by adding or subtracting pixels according to your need.

Maybe this works for you.

рüффп
  • 5,172
  • 34
  • 67
  • 113
Vivek
  • 10,978
  • 14
  • 48
  • 66