JQUERY PAGE SCROLL


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

Any link with a hash (#) will scroll to that id on the page. Hat Tip, Karl Swedberg.


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. //back to top scroll function. Any link with a hash (#) will scroll to that id on the page
  4. $('a[href*=#]').click(function() {
  5. if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
  6. && location.hostname == this.hostname) {
  7. var $target = $(this.hash);
  8. $target = $target.length && $target
  9. || $('[name=' + this.hash.slice(1) +']');
  10. if ($target.length) {
  11. var targetOffset = $target.offset().top;
  12. $('html,body')
  13. .animate({scrollTop: targetOffset}, 500);
  14. return false;
  15. }
  16. }
  17. });
  18. });
  19. </script>

URL: http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.