I have read that offsetLeft
and offsetTop
do not work properly in all browsers. jQuery.offset()
is supposed to provide an abstraction for this to provide the correct value xbrowser.
What I am trying to do is get the coordinates of where an element was clicked relative to the top-left of the element.
Problem is that jQuery.offset().top
is actually giving me a decimal value in FFX 3.6 (in IE and Chrome, the two values match).
This fiddle exhibits the issue. If you click the bottom image, jQuery.offset().top
returns 327.5, but offsetTop
returns 328.
I would like to think that offset()
is returning the correct value and I should use it because it will work across browsers. However, people obviously cannot click decimals of pixels. Is the proper way to determine the true offset to Math.round()
the offset that jQuery is returning? Should I use offsetTop
instead, or some other method entirely?
jQuery offset() Method The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.
The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.
Difference between offset() and position() Method:The jQuery UI offset() is relative to the document. The jQuery UI position() is relative to the parent element. When you want to position a new element on top of another one which is already existing, its better to use the jQuery offset() method.
The offsetHeight property includes the vertical padding and borders in the height calculation, therefore the . outerHeight() method would be the jQuery equivalent. $('. site-header').
This is what jQuery API Doc says about .offset()
:
Get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document.
This is what MDN Web API says about .offsetTop
:
offsetTop returns the distance of the current element relative to the top of the offsetParent node
This is what jQuery v.1.11 .offset()
basically do when getting the coords:
var box = { top: 0, left: 0 }; // BlackBerry 5, iOS 3 (original iPhone) if ( typeof elem.getBoundingClientRect !== strundefined ) { box = elem.getBoundingClientRect(); } win = getWindow( doc ); return { top: box.top + ( win.pageYOffset || docElem.scrollTop ) - ( docElem.clientTop || 0 ), left: box.left + ( win.pageXOffset || docElem.scrollLeft ) - ( docElem.clientLeft || 0 ) };
pageYOffset
intuitively says how much was the page scrolleddocElem.scrollTop
is the fallback for IE<9 (which are BTW unsupported in jQuery 2)docElem.clientTop
is the width of the top border of an element (the document in this case)elem.getBoundingClientRect()
gets the coords relative to the Conclusion
element.offsetTop
. Add element.scrollTop
if you want to take the parent scrolling into account. (or use jQuery .position() if you are fan of that library)element.getBoundingClientRect().top
. Add window.pageYOffset
if you want to take the document scrolling into account. You don't need to subtract document's clientTop
if the document has no border (usually it doesn't), so you have position relative to the document element.clientTop
if you don't consider the element border as the part of the elementI think you are right by saying that people cannot click half pixels, so personally, I would use rounded jQuery offset...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With