Skip to content

Instantly share code, notes, and snippets.

@ivan-marquez
Created January 7, 2019 11:41
Show Gist options
  • Save ivan-marquez/619bb85ebf5bfc516801812c736f0528 to your computer and use it in GitHub Desktop.
Save ivan-marquez/619bb85ebf5bfc516801812c736f0528 to your computer and use it in GitHub Desktop.
Detect when scroll reaches bottom of window.
export function scrolledToBottom(window, marginFromBottom = 0) {
const documentBody = window.document.body;
const documentElement = window.document.documentElement;
const scrollTop =
(documentElement && documentElement.scrollTop) || documentBody.scrollTop;
const scrollHeight =
(documentElement && documentElement.scrollHeight) ||
documentBody.scrollHeight;
const clientHeight = documentElement.clientHeight || window.innerHeight;
const bottomReached =
Math.ceil(scrollTop + clientHeight + marginFromBottom) >= scrollHeight;
return bottomReached;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment