diff options
Diffstat (limited to 'source/libs/anchor-scroll.ts')
-rw-r--r-- | source/libs/anchor-scroll.ts | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/source/libs/anchor-scroll.ts b/source/libs/anchor-scroll.ts new file mode 100644 index 00000000..559864da --- /dev/null +++ b/source/libs/anchor-scroll.ts @@ -0,0 +1,18 @@ +export default async function anchorScroll( + action: VoidFunction | AsyncVoidFunction, + anchor: Element = document.elementFromPoint(innerWidth / 2, innerHeight / 2)! +): Promise<void> { + if (anchor) { + const originalPosition = anchor.getBoundingClientRect().top; + + await action(); + + requestAnimationFrame(() => { + const newPositon = anchor.getBoundingClientRect().top; + window.scrollBy(0, newPositon - originalPosition); + }); + } else { + // Anchor not found; proceed without anchoring + action(); + } +} |