Answers for "smooth scroll anchor click js"

7

javascript smooth scroll to anchor element

//add smooth scrolling when clicking any anchor link
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});
//<a href="#someOtherElementID"> Go to Other Element Smoothly </a>
Posted by: Guest on August-02-2019
1

smooth scroll on clicking link

html{scroll-behavior:smooth}
Posted by: Guest on May-25-2021

Code answers related to "smooth scroll anchor click js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language