diff --git a/src/modules/dom.ts b/src/modules/dom.ts index ddb0ceb4..cc3d3fd4 100644 --- a/src/modules/dom.ts +++ b/src/modules/dom.ts @@ -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 */ @@ -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);