add scroll to top compatibility with other browsers

This commit is contained in:
Mina Saleeb 2020-01-27 10:14:27 -05:00
parent efea91f98e
commit 31616490e0
No known key found for this signature in database
GPG key ID: 58EB43C7DBE298F1

View file

@ -24,6 +24,14 @@ const BackToTopLink = styled.a`
}
`;
function scrollTopMax() {
let ref;
// scrollTopMax is only on Firefox right now! https://caniuse.com/#search=scrolltopmax
return (ref = document.scrollingElement.scrollTopMax) != null
? ref
: document.scrollingElement.scrollHeight - document.documentElement.clientHeight
}
function useScrollPosition() {
const [percent, setPercent] = useState(0);
@ -31,7 +39,7 @@ function useScrollPosition() {
console.log(document.documentElement.scrollTop);
const howFar =
document.documentElement.scrollTop /
document.documentElement.scrollTopMax;
scrollTopMax();
setPercent(howFar);
}