Posted By

zachdunn on 05/12/10


Tagged

jquery animation scrolling


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Ethyde
dubogii


Smooth Scrolling


 / Published in: jQuery
 

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

  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  

You need to login to post a comment.