jQuery smooth scroll to an anchor


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



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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.