Skip to content

Commit

Permalink
feat: add top offset up to the body
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-deriv committed Oct 26, 2023
1 parent 721dfef commit 4592ce2
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/modules/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ export function getElementPosition(
return Math.floor(top - offset);
}

/**
* Get the added offsetTop prop of each offsetParent up to the the body
*/
function getAddedOffsetTop(element?: HTMLElement | null): number {
if (element instanceof HTMLElement) {
if (element.offsetParent instanceof HTMLElement) {
return getAddedOffsetTop(element.offsetParent) + element.offsetTop;
}

return element.offsetTop;
}

return 0;
}

/**
* Get the scrollTop position
*/
Expand All @@ -202,10 +217,10 @@ export function getScrollTo(element: HTMLElement, offset: number, skipFix: boole
}

const parent = scrollParent(element);
let top = element.offsetTop;
let top = getAddedOffsetTop(element);

if (parent && hasCustomScrollParent(element, skipFix) && !hasCustomOffsetParent(element)) {
top -= parent.offsetTop;
top -= getAddedOffsetTop(parent);
}

return Math.floor(top - offset);
Expand Down

0 comments on commit 4592ce2

Please sign in to comment.