Advertisement
salmancreation

Performs a smooth page scroll to an anchor on the same page

Aug 7th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Smooth Scrolling
  2.  
  3. $(function() {
  4.   $('a[href*=#]:not([href=#])').click(function() {
  5.     if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
  6.       var target = $(this.hash);
  7.       target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
  8.       if (target.length) {
  9.         $('html,body').animate({
  10.           scrollTop: target.offset().top
  11.         }, 1000);
  12.         return false;
  13.       }
  14.     }
  15.   });
  16. });
  17.  
  18. https://github.com/davist11/jQuery-One-Page-Nav
  19.  
  20.  
  21. A lightweight jQuery plugin for the navigation on one-page sites. Adds smooth scrolling when clicking on the navigation and automatically selects the correct navigation items as you are scrolling through the different sections.
  22.  
  23. The plugin still works even if you add additional content to the page after the fact that changes the position of each section.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement