Skip to content

Instantly share code, notes, and snippets.

@HamedFathi
Last active November 11, 2020 14:34
Show Gist options
  • Save HamedFathi/6648911d06ad12ba0d44867837a123a8 to your computer and use it in GitHub Desktop.
Save HamedFathi/6648911d06ad12ba0d44867837a123a8 to your computer and use it in GitHub Desktop.
function scrollUp(start?: Function, end?: Function) {
const el = (document.documentElement || document.body);
if (start) start();
el.scroll({
top: 0,
left: 0,
behavior: 'smooth'
});
if (end) end();
}
function scrollBottom(start?: Function, end?: Function) {
const scrollingElement = (document.scrollingElement || document.body);
const el = (document.documentElement || document.body);
if (start) start();
const smoothScroll = (h: number) => {
let i = h || 0;
if (i < scrollingElement.scrollHeight) {
setTimeout(() => {
el.scrollTo(0, i);
smoothScroll(i + 10);
}, 5);
if (end) end();
}
}
smoothScroll(0);
}
function scrollBottomFast(start?: Function, end?: Function) {
const scrollingElement = (document.scrollingElement || document.body);
const el = (document.documentElement || document.body);
if (start) start();
el.scrollTo(0, scrollingElement.scrollHeight);
if (end) end();
}
export { scrollUp, scrollBottom, scrollBottomFast }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment