Smooth Page Scroll to Div on Same Page
Using hash tags to reference different parts of a page? Use this snippet to make the scroll to page smooth and to remove the hash tag that automatically gets appended to the url in the browser window.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html,body').animate({ scrollTop: target.offset().top }, 1000); return false; } } }); }); |