By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,237 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,237 IT Pros & Developers. It's quick & easy.

element position in nested tables??!!

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


tr***********@hotmail.com wrote:

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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.