Java # smooth scroll to element


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



Copy this code and paste it in your HTML
  1. $(function(){
  2.  
  3. $('a[href*=#]').click(function() {
  4.  
  5. if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
  6.  
  7.  
  8.  
  9. && location.hostname == this.hostname) {
  10.  
  11. var $target = $(this.hash);
  12.  
  13. $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
  14.  
  15.  
  16.  
  17.  
  18. if ($target.length) {
  19.  
  20. var targetOffset = $target.offset().top;
  21.  
  22. $('html,body').animate({scrollTop: targetOffset}, 1000);
  23.  
  24.  
  25.  
  26.  
  27. return false;
  28.  
  29. }
  30.  
  31. }
  32.  
  33. });
  34.  
  35. });

URL: http://elziah.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.