element position in nested tables??!!

T

travis_brooks

greetings

I had a javascript that was reliably telling me the position of an
image on a page, and now its not. I was using the script to draw a
bounding box that followed the mouse around over the image, and when
you clicked the mouse the x-y position of the image pixel the mouse was
over was posted back in the form. I just changed the layout of the
page so that the image is now in a table that is nested in another
table, and the old reliable script is now giving me the position of the
image as the upper left corner of the outer table a couple hundred
pixels in the wrong direction. When i first wrote the script i googled
around and people were saying you had to traverse up the document tree
to find the top (document level) offsetParent element to determine the
true offsetLeft and offsetTop of an element, which is what i did, and
which is what seemed to work for a while. BUT, once i nested these
tables that doesn't seem to work. Is this a common problem, or have i
just screwed up somewhere??

Here's the script i was using to find the position:

////

function Point(xpos, ypos){
this.x=xpos;
this.y=ypos;
}

function findPosition(element){
var currPt = new Point(0, 0);
if(element.offsetParent){
// just march up the document tree
while(element.offsetParent){
currPt.x = element.offsetLeft;
currPt.y = element.offsetTop;
element = element.offsetParent;
}
}else{
if(element.x)
currPt.x = element.x;
if(element.y)
currPt.y = element.y;
}
return currPt;
}

////

-thanks

-travis
 
M

Martin Honnen

function findPosition(element){
var currPt = new Point(0, 0);
if(element.offsetParent){
// just march up the document tree
while(element.offsetParent){

Try
while (element) {
currPt.x = element.offsetLeft;
currPt.y = element.offsetTop;
element = element.offsetParent;
}

otherwise your code misses out on the last element in the offset
hierarchy I think.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top