html - JavaScript: expand div to max height without getting scroll bars -
i working on document structure this:
<html> <head> <script src="http:http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript" charset="utf-8"></script> </head> <body> <h1>title</h1> <div id="main"> <div id="head">some text</div> <div id="content"><canvas id="canvas"></canvas></div> <div id="foot">some more text (and table)</div> </div> </body> </html>
this is, of course, bit simplified, shows real structure of document.
now, want extend #canvas
document wide , high possible without getting out of viewport (i.e., without getting scroll bars on document).
css-only solutions not o.k. @ least 2 reasons:
- this done dynamically, on request. depending on other factors, user may chose content of
#canvas
bigger or smaller that. want "make optimal size" button (i know how make button -- determining sizes). - the canvas element not end determined size, changed, multiplier of parameter, i.e., if
height
maximal allowable height compute, canvas height setfactor * math.floor(height / factor)
.
i have managed proper width using jquery:
canvas.width = 0; canvas.height = 0; var t = $(canvas); var w = $(window).width() - (t.outerwidth(true) - t.width()); $(canvas).parents().map(function() { t = $(this); w -= t.outerwidth(true) - t.width(); });
this takes width of viewport , chips away borders, paddings , margins of #canvas
, parents, after setting canvas
size 0x0 (to prevent miscalculations if wide , document has scrollbars). works intended, however, i've been unsuccessful in doing similar height (and making work , modern browsers).
among other things, have tried using elements' position().top
(which seems behave differently in seamonkey, chrome , firefox) , i've tried playing $(window)
/$(document)
/other elements' height()
, innerheight()
, outerheight()
, none of these worked.
edit:
maybe additional explanation help. canvas gameboard. think chess (although not chess). so, user can chose play on grid of mxn tiles (m,n being integers chosen via jqueryui spins; each tile factor
wide , high). so, want give user button "make board big possible without me needing scroll while playing".
edit (a possible solution)
this seems trick:
h = window.innerheight - $(main).position().top - $(main).outerheight(true);
i've tested under seamonkey (where $(window).height()
not working expected), firefox , chrome. have no way of testing ie newer ie8 (where, expected, canvas doesn't work).
Comments
Post a Comment