Re: Finding the V offset of an element
Hans-Georg Michna wrote:
> On Wed, 4 Apr 2012 04:57:56 -0700 (PDT), Richard Cornford wrote:
>
>> There are no reliable general methods (and the ones that get
>> closest to being general are quite complex), but given any
>> real-world context the odds are that a safe and clean method
>> can be created to do the job.
>
> So the following would not work reliably?
>
> function absTop(el) {
> var t = 0;
> do t += el.offsetTop;
> while (el = el.offsetParent);
> return t;
> }
For some values of "reliable", yes. It certainly is not a general
solution for providing an accurate value for the total pixel offset of
an element within a displayed document in all contexts. There are other
factors that this will not take account for. For example; the possible
non-zero (top/left) border widths of any element within the chain of -
offsetParent -s. The - offsetTop/Left - is the offset from the inside of
the containing element to the top/left pixel of the contained element,
where the inside is within any borders and the top/left pixel is the
top/left of any border. However, there are contexts where none of the
elements in the - offsetParent -s chain have borders, padding or
margins, where that code might be entirely suitable.
Richard.
|