-
-
Save hoyangtsai/a0d8f9db820380601a245a031f254495 to your computer and use it in GitHub Desktop.
Smooth scroll to element - Pure JS
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
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