summaryrefslogtreecommitdiff
path: root/source/libs/anchor-scroll.ts
blob: ef50f56e75a6182c1f24ae3d6d2be7ade6110f56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
export default function anchorScroll(
	anchor: Element = document.elementFromPoint(innerWidth / 2, innerHeight / 2)!
): VoidFunction {
	const originalPosition = anchor.getBoundingClientRect().top;

	/**
	Resets the previously-saved scroll
	*/
	return () => {
		requestAnimationFrame(() => {
			const newPosition = anchor.getBoundingClientRect().top;
			window.scrollBy(0, newPosition - originalPosition);
		});
	};
}