Skip to content

Commit cbe402e

Browse files
authored
Merge pull request #365 from cal-smith/position
fix(position): just use body as a reference
2 parents 379cc08 + b2a1272 commit cbe402e

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

src/utils/position.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,15 @@ export namespace position {
9393
}
9494

9595
export function getAbsoluteOffset(target: HTMLElement): Offset {
96-
let positionedElement;
9796
let currentNode = target;
9897
let margins = {
9998
top: 0,
10099
left: 0
101100
};
102101

103-
// searches either for a parent `positionedElement` or for
104-
// containing elements with additional margins
105-
// once we have a `positionedElement` we can stop searching
106-
// since we use offsetParent we end up skipping most elements
107-
while (currentNode.offsetParent && !positionedElement) {
102+
// searches for containing elements with additional margins
103+
while (currentNode.offsetParent) {
108104
const computed = getComputedStyle(currentNode.offsetParent);
109-
if (computed.position !== "static") {
110-
positionedElement = currentNode.offsetParent;
111-
}
112-
113105
// find static elements with additional margins
114106
// since they tend to throw off our positioning
115107
// (usually this is just the body)
@@ -129,14 +121,8 @@ export namespace position {
129121
currentNode = currentNode.offsetParent as HTMLElement;
130122
}
131123

132-
// if we don't find any `relativeElement` on our walk
133-
// default to the body
134-
if (!positionedElement) {
135-
positionedElement = document.body;
136-
}
137-
138124
const targetRect = target.getBoundingClientRect();
139-
const relativeRect = positionedElement.getBoundingClientRect();
125+
const relativeRect = document.body.getBoundingClientRect();
140126
return {
141127
top: targetRect.top - relativeRect.top + margins.top,
142128
left: targetRect.left - relativeRect.left + margins.left

0 commit comments

Comments
 (0)