Skip to content

Instantly share code, notes, and snippets.

@hoyangtsai
Forked from benhatsor/scrollToEl.js
Created August 18, 2020 09:36
Show Gist options
  • Save hoyangtsai/a0d8f9db820380601a245a031f254495 to your computer and use it in GitHub Desktop.
Save hoyangtsai/a0d8f9db820380601a245a031f254495 to your computer and use it in GitHub Desktop.
Smooth scroll to element - Pure JS
function scrollToEl(el) {
var rect = el.getBoundingClientRect(),
scrollTop = window.pageYOffset || document.documentElement.scrollTop,
scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
elTop = rect.top + scrollTop,
elLeft = rect.left + scrollLeft;
window.scrollTo({
top: elTop,
left: elLeft,
behavior: 'smooth'
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment