jQuerys offset function sometimes returns rational numbers (like 12.645613) for top or left. I thought that top and left positions are in pixels and so should be integers (there are no half pixel, or?).
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.
jQuery offset() MethodThe 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 offset() method retrieves the current position relative to the document, whereas the position() method retrieves the current position of an element relative to the parent element.
Top and left positions can be floating point numbers with any of the units cm, mm, in, pt, pc, em, ex or px, or percentages.
Example:
.someElement { top: 42%; left: 3.14in; }
The offset
function returns the position translated into pixels, so that can very well be a floating point number. The values are not rounded.
With the example given, if the height of the parent would for example be 32 pixels, the top
value of the element would be 32 * 0.42 = 13.44 pixels.
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