diff options
author | 2019-08-01 23:12:46 +0530 | |
---|---|---|
committer | 2019-08-02 00:42:46 +0700 | |
commit | f36d4f10c725b4511e0d28d5c1fec2cf1b2a22b6 (patch) | |
tree | fdfa7f5c02cfe77f3c02bcc744b340ae3ebd28a5 /source/libs/anchor-scroll.ts | |
parent | e03473b5f08333d00f052578361b78b54d3eab7c (diff) | |
download | refined-github-f36d4f10c725b4511e0d28d5c1fec2cf1b2a22b6.tar.gz refined-github-f36d4f10c725b4511e0d28d5c1fec2cf1b2a22b6.tar.zst refined-github-f36d4f10c725b4511e0d28d5c1fec2cf1b2a22b6.zip |
Add `minimize-user-comments` feature (#2146)
Co-Authored-By: Federico Brigante <github@bfred.it>
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(); + } +} |