Smooth Scrolling


/ Published in: jQuery
Save to your folder(s)

Makes same-page links scroll smoothly instead of jumping around the page.


Copy this code and paste it in your HTML
  1. //Smooth scrolling within page anchors
  2. $('a[href*=#]').click(function() {
  3.  
  4. if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
  5. && location.hostname == this.hostname) {
  6.  
  7. var $target = $(this.hash);
  8.  
  9. $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
  10.  
  11. if ($target.length) {
  12. var targetOffset = $target.offset().top;
  13. $('html,body').animate({scrollTop: targetOffset}, 1000);
  14. return false;
  15. }
  16. }
  17. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.