Skip to content

Instantly share code, notes, and snippets.

@objectfoo
Last active July 12, 2021 03:25
Show Gist options
  • Select an option

  • Save objectfoo/f5a49ef5aeeadb45586e150dd00d30ec to your computer and use it in GitHub Desktop.

Select an option

Save objectfoo/f5a49ef5aeeadb45586e150dd00d30ec to your computer and use it in GitHub Desktop.
export default function smoothScrollTop(): Promise<null> {
let lastScrollTop = Infinity;
return new Promise((resolve) => {
const smoothScrollTopImpl = () => {
const top = document.documentElement.scrollTop || document.body.scrollTop;
if (top > 0 && lastScrollTop > top) {
lastScrollTop = top;
window.requestAnimationFrame(smoothScrollTopImpl);
window.scrollTo(0, top - (top / 8));
} else {
lastScrollTop = Infinity;
resolve(null);
}
};
setTimeout(() => smoothScrollTopImpl(), 10);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment