Merge pull request #561 from saleebm/compat/back-to-top

add scroll to top compatibility with other browsers
This commit is contained in:
Wes Bos 2020-01-27 15:34:15 -05:00 committed by GitHub
commit 80c7a41d21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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() { function useScrollPosition() {
const [percent, setPercent] = useState(0); const [percent, setPercent] = useState(0);
@ -31,7 +39,7 @@ function useScrollPosition() {
console.log(document.documentElement.scrollTop); console.log(document.documentElement.scrollTop);
const howFar = const howFar =
document.documentElement.scrollTop / document.documentElement.scrollTop /
document.documentElement.scrollTopMax; scrollTopMax();
setPercent(howFar); setPercent(howFar);
} }