Demo page at http://csaba.org/Demos/getRect.htm
One question I've seen come up multiple times is:
How can I find the abolute position of my favorite element
on the page? The standard answer is not working for me
on any of IE/Netscape/Opera! The above page demonstrates
the problem. I'd love to know how to make it work correctly.
The usual answer, which I have been using, too, is something like:
function getRect(elem) {
// return (Left,Top,Right,Bottom) of the element
var myRect = Array(elem.offsetLeft, elem.offsetTop,
elem.offsetLeft + elem.offsetWidth,
elem.offsetTop + elem.offsetHeight)
if (!elem.offsetParent) return myRect;
newRect = getRect(elem.offsetParent)
for (var i=0;i<4;i++) myRect[i] = myRect[i] + newRect[i % 2];
return myRect;
}
This is close, but I am not doing government work, and I need something
exact. Specifically, As I move my mouse around a table, whenever the
mouse passes over a table border, I want to be able to identify which
two cells it is between. Let's leave that problem alone for now, since it's
much more difficult (when rowspan/colspan are involved) and just
concentrate on figuring out when the mouse is over an external border.
Now the mouse already knows which element it is over, and you can see
this as the mouse changes form as it goes from one cell, over
a border, then to another.
So the plan is that whenever the table gets an onmouseover event,
we'll compare the mouse position to the top left TD and the bottom
right TD element to find out if we're at an external border.
My IE 5.5, NN 6.1 and Opera 7.01 on Win 2K Pro all fail these
comparisons on exactly two borders each, and we are talking that there
is a two pixel difference! The observed behaviour is that when you
approach the border from the outside, it always works but going from
the inside to the outside of the table it fails on two borders because of
the two pixel difference.
Can anyone spot how to fix this problem up?
Thanks for any tips,
Csaba Gabor from New York
PS. OK, the code to test for the right border cannot assume that the
last cell of the last row is rightmost in the table, but let's not worry
about that till after the main problem is solved.