Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non integer offset positions in jQuery

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?).

like image 291
Zardoz Avatar asked Jan 07 '11 14:01

Zardoz


People also ask

What is the difference between position and offset in jQuery?

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.

How offset works in jQuery?

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.

What is the difference between position and offset?

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.


1 Answers

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.

like image 132
Guffa Avatar answered Sep 22 '22 18:09

Guffa