views:

128

answers:

1

Im looking for a way, in javascript, to calculate the size of the browser (px) then calculate the size of a <div> taking away 50px from that full screen size:

E.g.

Browser screen size: 800px (Height)
Existing <div> that is always 50px (Height)
Leaves 750px (Height) for the remaining <div> to fill the page.

Then take that 750px and apply it as inline style:

<div style="height: 50px">
<img src="banner.png" />
</div>

<div style="height: x">
This fills the remainder of the page
</div>
+4  A: 

I assume you mean document height, not browser height. You can do this using something like the following:

$("#div2").height($(document).height() - 50);

// or for more dynamicity
$("#div2").height($(document).height() - $("#div1").height());    

If you did mean browser height and not document height, substitute $(document) with $(window).

Andy E
Can you add another element to subtract: $("#div2").height($(document).height() - $("#div1") - $("#div3").height());
danit
@danit: if you want to, sure. Add as many elements as you like. Make sure you're calling the `height()` method on them, though -- eg. `$("#div2").height($(document).height() - $("#div1").height() - $("#div3").height());`
Andy E
Thanks, another question. Possible to resize #div2 when the document is resized? Tried .resize but thats for the window not the document.
danit
@danit, when the window resizes, so should the document. So you would use `$(window).resize(function () { /* do calculation here */ }`. Apparently, in Firefox the `resize` event will only fire when you finish resizing. In other browsers it fires as you are dragging the window.
Andy E