\r
/**\r
* Get the current offset of the first matched element, in pixels, relative to\r
- * the document. The returned object contains two Float properties, top and\r
- * left. Browsers usually round these values to the nearest integer pixel for\r
- * actual positioning. The method works only with visible elements.\r
+ * the document. The returned object contains two integer properties, top and\r
+ * left. The method works only with visible elements.\r
*/\r
public Offset offset() {\r
- if (length() == 0) {\r
- return new Offset(0, 0);\r
- }\r
- int boxtop = getClientBoundingRectTop(get(0));\r
- int boxleft = getClientBoundingRectLeft(get(0));\r
- Element docElem = document.getDocumentElement();\r
- Element body = document.getBody();\r
- int clientTop = docElem.getPropertyInt("clientTop");\r
- if (clientTop == 0) {\r
- clientTop = body.getPropertyInt("clientTop");\r
- }\r
- int clientLeft = docElem.getPropertyInt("clientLeft");\r
- if (clientLeft == 0) {\r
- clientLeft = body.getPropertyInt("clientLeft");\r
- }\r
- int wleft = Window.getScrollLeft();\r
- int wtop = Window.getScrollTop();\r
- int top = boxtop + (wtop == 0 ? body.getScrollTop() : wtop) - clientTop;\r
- int left = boxleft + (wleft == 0 ? body.getScrollLeft() : wleft)\r
- - clientLeft;\r
- return new Offset(top, left);\r
+ return new Offset(get(0).getAbsoluteTop(), get(0).getAbsoluteLeft());\r
}\r
\r
/**\r
* and padding. This method only works with visible elements.\r
*/\r
public Offset position() {\r
-\r
- if (size() > 0) {\r
- GQuery offsetParent = offsetParent();\r
- Offset offset = offset();\r
- Element e = offsetParent.get(0);\r
-\r
- Offset parentOffset = BodyElement.is(e) || "html".equals(e.getTagName())\r
- ? new Offset(0, 0) : offsetParent.offset();\r
- offset.top -= num(this, "marginTop");\r
- offset.left -= num(this, "marginLeft");\r
- parentOffset.top += num(offsetParent, "borderTopWidth");\r
- parentOffset.left += num(offsetParent, "borderLeftWidth");\r
- return new Offset(offset.top - parentOffset.top,\r
- offset.left - parentOffset.left);\r
- }\r
- return null;\r
+ return new Offset(get(0).getOffsetTop(), get(0).getAbsoluteLeft());\r
}\r
\r
/**\r
return styleImpl.getPropertyName(key);\r
}\r
\r
- private native int getClientBoundingRectLeft(Element element) /*-{\r
- return element.getClientBoundingRect().left;\r
- }-*/;\r
-\r
- private native int getClientBoundingRectTop(Element element) /*-{\r
- return element.getClientBoundingRect().top;\r
- }-*/;\r
-\r
private native Document getContentDocument(Node n) /*-{\r
return n.contentDocument || n.contentWindow.document;\r
}-*/;\r