Determining position

L

Let_Me_Be

I have some floating divs on my page.

When I click the div I want do display a menu over it, how can I
determine the position of the clicked div on the page?
 
P

petermichaux

Let_Me_Be said:
I have some floating divs on my page.

When I click the div I want do display a menu over it, how can I
determine the position of the clicked div on the page?

Richard Cornford wrote about the difficulty in doing this in general

<URL:
http://groups.google.com/group/comp.lang.javascript/msg/c46e8ec2f5fe4db3?>

Richard, if you happen to be reading, is there a chance you could post
a few of the more basic positioning moduals you have written. Or
perhaps you have a link to one of your pages which uses them?

Thank you,
Peter
 
B

Benjamin

try this:
function posMenu(menu, menuBase) {
var offsetObj;
var offsetX, offsetY = 0;
offsetObj = menuBase;
while (offsetObj) {
offsetX += offsetObj.offsetTop;
offsetY += offsetObj.offsetLeft;
offsetObj = offsetObj.offsetParent;
}
menu.style.position = "absulote";
menu.style.top = offsetX + "px";
menu.style.left = offsetY + "px";
}
 
P

petermichaux

Benjamin said:
try this:
function posMenu(menu, menuBase) {
var offsetObj;
var offsetX, offsetY = 0;

should be this?
var offsetX = 0, offsetY = 0;


offsetObj = menuBase;
while (offsetObj) {
offsetX += offsetObj.offsetTop;
offsetY += offsetObj.offsetLeft;
offsetObj = offsetObj.offsetParent;
}
menu.style.position = "absulote";

Should be "absolute"
menu.style.top = offsetX + "px";
menu.style.left = offsetY + "px";
}

I haven't written a positioning function more advanced then the one you
have shown but looking at the Yahoo! UI position function it looks like
the proposed function will only work in a limited set of simple cases
(ie. no frames etc).

Peter
 
B

Benjamin

It is true that it will only work in simple cases (if that's simple),
but I assumed that is what he wanted.
 
R

RobG

should be this?
var offsetX = 0, offsetY = 0;




Should be "absolute"


I haven't written a positioning function more advanced then the one you
have shown but looking at the Yahoo! UI position function it looks like
the proposed function will only work in a limited set of simple cases
(ie. no frames etc).

Matt Kruse has a reasonable effort as part of his JavaScript toolkit:

<URL: http://www.javascripttoolbox.com/lib/objectposition/ >


There is a simpler version at Quirksmode:

<URL: http://www.quirksmode.org/js/findpos.html >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top