Search code examples
javascriptjquerypositioningdimensions

How do I easily find the distance between a point on the page and the bottom of the browser window using JavaScript?


A view in my web app has a table which may be extremely long, so I wrapped it in a div with overflow: auto; max-height: 400px; so users can scroll through it while keeping the other controls on the page visible.

I want to use a bit of JavaScript to dynamically adjust the max-height CSS property so the div stretches to the bottom of the browser window. How can I determine this value? jQuery solutions are fine.

The table doesn't start at the top of the page, so I can't just set the height to 100%.


Solution

  • Something like this would work I think:

    var topOfDiv = $('#divID').offset().top;
    var bottomOfVisibleWindow = $(window).height();
    $('#divID').css('max-height', bottomOfVisibleWindow - topOfDiv - 100);