aboutsummaryrefslogtreecommitdiffstats
path: root/src/offset.js
diff options
context:
space:
mode:
authorArthur Stolyar <nekr.fabula@gmail.com>2015-05-05 08:16:29 -0700
committerTimmy Willison <timmywillisn@gmail.com>2015-05-05 08:16:29 -0700
commit1617479fcf7cbdaf33dc9334ed10a0f30bf14687 (patch)
treee605b68a8f6b68c8b62e4b7751eedf89362f8f1c /src/offset.js
parentadd85afed5944ec10d68ca10e91421e031fe0a5d (diff)
downloadjquery-1617479fcf7cbdaf33dc9334ed10a0f30bf14687.tar.gz
jquery-1617479fcf7cbdaf33dc9334ed10a0f30bf14687.zip
Offset: Fix .offset() to correctly work with ShadowDOM
Fixes gh-1784 Close gh-2043
Diffstat (limited to 'src/offset.js')
-rw-r--r--src/offset.js26
1 files changed, 12 insertions, 14 deletions
diff --git a/src/offset.js b/src/offset.js
index a0d3ab5fb..49ff26ac9 100644
--- a/src/offset.js
+++ b/src/offset.js
@@ -32,7 +32,7 @@ jQuery.offset = {
elem.style.position = "relative";
}
- curOffset = curElem.offset();
+ curOffset = curElem.offset() || { top: 0, left: 0 };
curCSSTop = jQuery.css( elem, "top" );
curCSSLeft = jQuery.css( elem, "left" );
calculatePosition = ( position === "absolute" || position === "fixed" ) &&
@@ -82,28 +82,26 @@ jQuery.fn.extend({
});
}
- var docElem, win,
+ var docElem, win, rect,
elem = this[ 0 ],
- box = { top: 0, left: 0 },
doc = elem && elem.ownerDocument;
if ( !doc ) {
return;
}
- docElem = doc.documentElement;
+ rect = elem.getBoundingClientRect();
- // Make sure it's not a disconnected DOM node
- if ( !jQuery.contains( docElem, elem ) ) {
- return box;
- }
+ // Make sure element is not hidden (display: none) or disconnected
+ if ( rect.width || rect.height || elem.getClientRects().length ) {
+ win = getWindow( doc );
+ docElem = doc.documentElement;
- box = elem.getBoundingClientRect();
- win = getWindow( doc );
- return {
- top: box.top + win.pageYOffset - docElem.clientTop,
- left: box.left + win.pageXOffset - docElem.clientLeft
- };
+ return {
+ top: rect.top + win.pageYOffset - docElem.clientTop,
+ left: rect.left + win.pageXOffset - docElem.clientLeft
+ };
+ }
},
position: function() {