Smooth Scroll to ID with jQuery

Here's a quick snippet of jQuery code I use often when I need to smoothly scroll to an ID. Just change the 500 to whatever speed (in milliseconds) you want the page to scroll at.

View Demo

$('a[href*="#"]').on('click', function(e) {
  e.preventDefault()

  $('html, body').animate(
    {
      scrollTop: $($(this).attr('href')).offset().top,
    },
    500,
    'linear'
  )
})

Leave a comment

Plain text only. Comment must be over 20 characters.

Join the newsletter

I write about JavaScript, programming, and front-end design. Join other developers in keeping up with my content. Unsubscribe whenever. Never any spam, ads, or affiliate links.