Skip to content

Instantly share code, notes, and snippets.

@FragsterAt
Created July 2, 2019 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FragsterAt/b72632976cac00fc237578a730ae2177 to your computer and use it in GitHub Desktop.
Save FragsterAt/b72632976cac00fc237578a730ae2177 to your computer and use it in GitHub Desktop.
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