Created
July 2, 2019 15:40
-
-
Save FragsterAt/b72632976cac00fc237578a730ae2177 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function scrollToElement(domEl) { | |
if (domEl) { | |
let offset = Math.min( | |
Math.round((domEl.offsetHeight - window.innerHeight) / 2), | |
0 | |
); | |
while (domEl.offsetParent) { | |
// console.log(domEl, domEl.offsetParent); | |
offset += domEl.offsetTop; | |
domEl = domEl.offsetParent; | |
} | |
if (document.documentElement.scroll) { | |
document.documentElement.scroll({ | |
top: offset, | |
left: 0, | |
behavior: "smooth" | |
}) | |
} else { | |
document.body.scrollTop = offset; | |
document.body.scrollLeft = 0; | |
} | |
} | |
} | |
export function scrollToTop() { | |
if (document.documentElement.scroll) { | |
document.documentElement.scroll({ | |
top: 0, | |
left: 0, | |
behavior: "smooth" | |
}) | |
} else { | |
document.body.scrollTop = 0; | |
document.body.scrollLeft = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment