Skip to content

Latest commit

History

History
30 lines (26 loc) 路 648 Bytes

2017-04-14-smooth-scroll-to-id-with-jquery.md

File metadata and controls

30 lines (26 loc) 路 648 Bytes
date title template thumbnail slug categories tags
2017-04-14
Smooth Scroll to ID with jQuery
post
../thumbnails/jquery.png
smooth-scroll-to-id-with-jquery
Code
javascript
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'
  )
})