DEV Community

Atta
Atta

Posted on • Edited on • Originally published at attacomsian.com

6 1

Smooth scroll to page section with jQuery

This post was originally published on attacomsian.com/blog.


For one page templates and websites, it is a common practice to scroll to a page section when clicking on an anchor link. Here is a little jQuery hack I often use to smoothly scroll to a page section when a visitor clicks on the anchor link in the navigation menu (or anywhere else in the page). Adjust the scroll speed value 1000 to whatever the speed you want to. This value is in milliseconds.

$(document).on('click', 'a[href^="#"]', function (e) {
    e.preventDefault();
    $('html, body').stop().animate({
        scrollTop: $($(this).attr('href')).offset().top
    }, 1000, 'linear');
});

Here is how the HTML markup looks like for navigation and sections:

<nav>
  <a href="#features">Features</a>
  <a href="#faq">FAQ</a>
  <a href="#pricing">Pricing</a>
</nav>
<!--main container-->
<div class="container">
    <!--feature section-->
    <section id="features">...</section>
    <!--faq section-->
    <section id="faq">...</section>
    <!--pricing section-->
    <section id="pricing">...</section>
</div>

Don't want to use jQuery? You can use vanilla JavaScript too for smooth scrolling but it might not work in old browsers:

document.querySelectorAll('a[href^="#"]').forEach($anchor => {
    $anchor.addEventListener('click', function (e) {
        e.preventDefault();
        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth',
            block: 'start' //scroll to top of the target element
        });
    });
});

Not a big fan of vanilla JavaScript either? Here is pure CSS 3 solution but it only works in the latest browsers:

body {
    scroll-behavior: smooth;
}

✌️ I write about modern JavaScript, Node.js, Spring Boot, and all things web development. Subscribe to my newsletter to get web development tutorials & protips every week.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (1)

Collapse
 
yansusanto profile image
Yan Susanto

Hi Atta,

May I ask if it is possible to control the speed using vanilla JS?

Thanks!

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️