smooth scroll to div js code example
Example 1: javascript smooth scroll to anchor element
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
});
Example 2: smooth scroll css
html {
scroll-behavior: smooth;
}
http://iamdustan.com/smoothscroll/
Example 3: how to smooth scroll in javascript
window.scrollTo({ top: 900, behavior: 'smooth' })