Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I center a layer based on the window size?

Status
Not open for further replies.

debrac

IS-IT--Management
Jan 9, 2001
48
AU
How do I center a layer based on the current window size, so that when a user resizes the window, the position of the layer refreshes?

thanks.
 
this example worked in IE:

<div id=&quot;benluc&quot; style=&quot;position:absolute;width:200px;height:200px;background-color:red&quot;></div>

<script>
function centerElement(obj)
{
var ofsth = parseInt(obj.style.height)/2
var ofstw = parseInt(obj.style.width)/2
alert(ofsth)
obj.style.left = window.document.body.offsetWidth/2 - ofstw
obj.style.top = window.document.body.clientHeight/2 - ofsth
}
centerElement(benluc)
</script>

this works in IE, for a ns6 version, use document.getElementById and availHeight and availWidth jared@aauser.com
 
you will also need to apply this script to the resize handler of the window:

function someFunc()
{
centerElement(benluc)
}

window.onresize=someFunc jared@aauser.com
 
im a beginner javascripter. where do i put window.onresize....

thanks
 
just put my last post in script tags in the head, with my first post above it jared@aauser.com
 
but i want to use it for the other jscript (where it aligns the layer to the right) and so if i put this line in the head it doesnt recognise the layer which is defined in the body.
 
then set it onload:

<body onload=&quot;window.onresize=someFunc&quot;> jared@aauser.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top