Element.scrollIntoView()
to scroll the element.{ behavior: 'smooth' }
to scroll smoothly.const smoothScroll = element =>
document.querySelector(element).scrollIntoView({
behavior: 'smooth'
});
smoothScroll('#fooBar'); // scrolls smoothly to the element with the id fooBar
smoothScroll('.fooBar');
// scrolls smoothly to the first element with a class of fooBar
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️