/    Sign up×
Community /Pin to ProfileBookmark

Help with auto adjustable div box

Hello,

I need some help! I have a div box that sticks 10px away from the right and bottom edge of the browser window and auto adjusts itself when the browser is resized. It works perfectly, however, when the window is resized to it’s smallest size either virtically or horrizontally, I get a js error. Here is the code. Can someone please help!

[CODE]
<HTML>
<HEAD>
<TITLE>Example</TITLE>
<script type=”text/javascript” language=”javascript”>
<!–
function chk(){
//width setting
my_div=document.getElementById(“adjustBox”)
left_space=document.getElementById(“adjustBox”).offsetLeft
right_space=10
my_div.style.width=document.body.clientWidth-left_space-right_space

//height setting
my_div=document.getElementById(“adjustBox”)
top_space=document.getElementById(“adjustBox”).offsetTop
bottom_space=10
my_div.style.height=document.body.clientHeight-top_space-bottom_space
}
onresize=chk
onload=chk
//–>
</script>
<style type=”text/css”>
<!–
body {
overflow: hidden;
}
div#adjustBox {
/*position:absolute;
top: 0px;
left: 0px;*/
padding:10px;
width:100%;
height:100%;
overflow: auto;
background-color:#ccc;
overflow-x:auto;
border:1px solid #000;
}
–>
</style>
</HEAD>
<BODY>
<div style=”border:1px solid #517191; background-color:#6688A4; height:18px; width:100%;”>&nbsp;</div>
<div id=”adjustBox”>

</div>
</BODY>
</HTML>
[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Kostas_ZotosOct 17.2008 — Hi,

I think you can achieve the same results using only CSS (eg. percent sizes 100&#37; in conjunction with margin/padding settings)..

Anyway the problem seems to exist because the width and/or height gets very small or zero values..
-----------------



Use an if statement to change the value if becomes eg. less than a minimum value

Or may use something like this:
[CODE]// ... ...
function chk(){
//width setting
my_div=document.getElementById("adjustBox")
left_space=document.getElementById("adjustBox").offsetLeft
right_space=10
my_div.style.width=[B]Math.max(document.body.clientWidth-left_space-right_space,10)[/B] [COLOR="SeaGreen"]// gets the max of 2 values (minimum possible value is set to 10)[/COLOR]
//height setting
my_div=document.getElementById("adjustBox")
top_space=document.getElementById("adjustBox").offsetTop
bottom_space=10
my_div.style.height=[B]Math.max(document.body.clientHeight-top_space-bottom_space,10)[/B]
}
// ... ...[/CODE]


Kostas
Copy linkTweet thisAlerts:
@bigaloauthorOct 17.2008 — Thanks Kostas! I tried this and it worked perfectly!


Cheers!
×

Success!

Help @bigalo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...