473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to determine a postion (for a menu in a centered table)

I have a script for a menu. However, this menu uses absolute coordinates.
This menu had to be placed on a website. This website is position
(centered) using a table. How can I determine/calculate where to fix my
menuregardless the browser, slidebar width, tool etc.

Thanks

Ernst
Jul 23 '05 #1
3 1450
Ernst wrote:
I have a script for a menu. However, this menu uses absolute coordinates.
This menu had to be placed on a website. This website is position
(centered) using a table. How can I determine/calculate where to fix my
menuregardless the browser, slidebar width, tool etc.


The following functions might help. Pass a reference to your menu
element as argument. The return value will be an integer defining the
distance in pixel.

function getAbsoluteX (elm) {
var x = 0;
if (elm && typeof elm.offsetParent != "undefined") {
while (elm && typeof elm.offsetLeft == "number") {
x += elm.offsetLeft;
elm = elm.offsetParent;
}
}
return x;
}

function getAbsoluteY(elm) {
var y = 0;
if (elm && typeof elm.offsetParent != "undefined") {
while (elm && typeof elm.offsetTop == "number") {
y += elm.offsetTop;
elm = elm.offsetParent;
}
}
return y;
}

// usage:
var tableElm = document.getElementById("mainTable");
var x = getAbsoluteX(tableElm);
var y = getAbsoluteY(tableElm);

var menuElm = document.getElementById("menu");
menuElm.style.left = x + "px";
menuElm.style.top = y + "px";

Daniel
Jul 23 '05 #2
Daniel thanks for the effort.
I'm going to implement this.
Ernst wrote:
I have a script for a menu. However, this menu uses absolute coordinates.
This menu had to be placed on a website. This website is position
(centered) using a table. How can I determine/calculate where to fix my
menuregardless the browser, slidebar width, tool etc.


The following functions might help. Pass a reference to your menu
element as argument. The return value will be an integer defining the
distance in pixel.

function getAbsoluteX (elm) {
var x = 0;
if (elm && typeof elm.offsetParent != "undefined") {
while (elm && typeof elm.offsetLeft == "number") {
x += elm.offsetLeft;
elm = elm.offsetParent;
}
}
return x;
}

function getAbsoluteY(elm) {
var y = 0;
if (elm && typeof elm.offsetParent != "undefined") {
while (elm && typeof elm.offsetTop == "number") {
y += elm.offsetTop;
elm = elm.offsetParent;
}
}
return y;
}

// usage:
var tableElm = document.getElementById("mainTable");
var x = getAbsoluteX(tableElm);
var y = getAbsoluteY(tableElm);

var menuElm = document.getElementById("menu");
menuElm.style.left = x + "px";
menuElm.style.top = y + "px";


Jul 23 '05 #3
Ernst wrote:
I have a script for a menu. However, this menu uses absolute coordinates. This menu had to be placed on a website. This website is position
(centered) using a table. How can I determine/calculate where to fix my menuregardless the browser, slidebar width, tool etc.


Why use JavaScript for layout? That what CSS was created for.
Programming your layout should always be a last resort.

You can *relatively* position an absolutely positioned element (place it
in a relationship with another element) by wrapping both in a relatively
positioned 'container' and adjusting the coordinates (left: / top:).
Absolutely positioned elements get their positioning context (x, y) from
their next outer containing element, if that element is itself
positioned, or the body/html element if none is positioned (depending on
doctype). Try this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<style type="text/css">

html, body {
height: 100%;
margin: 0;
background: buttonface;
}
div#container {
position: relative;
width: 722px;
height: 100%;
margin: 0 auto;
}
div#menu {
position: absolute;
left: 4px;
top: 2px;
width: 100px;
height: 200px;
border: 3px white groove;
background: crimson;
overflow: hidden;
}
div#menu hr {
height: 8px;width:90%;
}
table#content {
width: 720px;
height: 100%;
border-right: 1px black dashed;
border-left: 1px black dashed;
background: pink;
}
td {
width: 50%;
height: 102px;
}

</style>
<script type="text/javascript">
//<![CDATA[

//]]>
</script>
</head>
<body>
<div id="container">
<div id="menu">
<hr /><hr /><hr /><hr />
<hr /><hr /><hr /><hr />
<hr /><hr /><hr /><hr />
</div>
<table id="content">
<tbody>
<tr>
<td></td>
</tr><tr>
<td></td>
</tr>
<tbody>
</table>
</div>
</body>
</html>

Just a demo, your mileage may vary depending on that doctype.

http://www.brainjar.com/css/positioning/default.asp

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve K | last post by:
HI, Can anyone figure out why the words in my menu bar hang out near the very top of the red bar in Mozilla? http://aem.cornell.edu/business_test_site/overview/ ...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
0
by: Fred Lock | last post by:
Does anyone know how to make the following centered floated "unknown width" menu into a fully-fledged single-level drop menu? (as copied from http://www.cssplay.co.uk/menus/centered.html): ...
2
by: jayson_13 | last post by:
Hi, I have a menu screen and i want to store the menus in a table. Is the a way that i can load the form according to the menu user has selected? Eg: Menu =================
40
by: Jeff | last post by:
I have a system on a network and want to determine if anyone is currently connected to the back-end files. An interesting twist is that I have noticed that some users can be connected (have the...
2
by: duzhidian | last post by:
Hello: Many books talk about how to the layout of html using the following sequence: wrapper menu bar (short and not long enough to expand the whole wrapper) left sidebar right sidebar...
4
by: Doug | last post by:
Hi I am using a form in an application that i have attached to the context menu using a program called right click configurator. The position of the applicaiton when called through the context...
5
by: totalstranger | last post by:
I'm working on a lightweight, auto width, auto centered, one level drop down menu. It's working in FF, IE7 and Safari (all windows versions on my local system), however it fails in Opera. Can...
2
by: Jeff | last post by:
hi asp.net 2.0 I wan thte menu to be horizontally centered on the webpage, but I want the submenuitems to left aligned. So I created a table cell with HorizontalAlign set to Center. With this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.