Email conversation
From | Ricardo Bravo |
To | Me |
Subject | suggestion: findPosition takes into account element(s) scroll |
Date | 2 April 2008 21:06 |
dear Tarquin,
I'd been using your findPosition script for a while, and I'd been very happy
with it (and grateful!).
I just came across with an element which overflows from its parent. when the
inside element is at 'zero position' the script works as usual, but after
element has been scrolled findPosition keeps returning the original
coordinates, regardless where the object is scrolled. in this way the
informed position does not agree with where the object is displayed on the
screen. so I made a small improvement to the script, which is now:
function findPosition( oElement ) {
if ( typeof( oElement.offsetParent ) != 'undefined' ) {
for ( var posX = oElement.scrollLeft, posY = oElement.scrollTop;
oElement; oElement = oElement.offsetParent ) {
posX += oElement.offsetLeft - oElement.scrollLeft;
posY += oElement.offsetTop - oElement.scrollTop;
}
return [ posX, posY ];
} else {
return [ oElement.x, oElement.y ];
}
}
as you can see the only modification is to start with scrollLeft instead of
0 and to substract scrollLeft to offsetLeft (the same with 'Top').
it works OK (on Windows XP SP2) with Explorer
6.0.2900.2180_xpsp_sp2_gdr.070227-2254, Firefox 2.0.0.13 and Safari 3.1
(525.13.3).
it fails with Opera 9.26 build 8835 (the reported position is to the left of
actual one, suggesting that offsetLeft already takes into account the
scrolled distance).
I hope you find this contribution useful... best regards,
Ricardo Bravo
From | Me |
To | Ricardo Bravo |
Subject | Re: suggestion: findPosition takes into account element(s) scroll |
Date | 3 April 2008 23:58 |
Ricardo,
> findPosition keeps returning the original coordinates
This is an interesting point, and deserves some attention.
> it fails with Opera
This, unfortunately is killer for me. I cannot recommend anything that
does not reliably work cross browser. If I can find a solution that can
work without needing any browser detection, I will add instructions into
the tutorial for it.
Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
From | Me |
To | Ricardo Bravo |
Subject | Re: suggestion: findPosition takes into account element(s) scroll |
Date | 4 April 2008 15:46 |
Ricardo,
> it fails with Opera 9.26
After some more investigations into this, it turns out that it only fails in
Opera for inline elements. It works correctly for block elements. This makes
it useful enough to use in most cases, which is great.
A more important point, however, is that it fails in various browsers, in
various ways, depending on the DOCTYPE, and on what overflow styles are
applied to the BODY and HTML. Basically the viewport scrollbar can come from
either element, and they can also have separate scrollbars depending on the
overflow style. Different browsers reflect the offset on different object
depending on which applies the scrollbar, and no two browsers are the same.
As a result, it becomes completely unreliable for scrolling offsets of the
BODY or HTML elements (ie. if the page itself has been scrolled). The
easiest solution is not to subtract the offset for the BODY or HTML
elements. These can be added on separately if needed by checking how far the
page has been scrolled. I will add a script to my tutorial with workarounds
for these issues.
Thanks for the suggestions.
Tarquin