The accepted answer did not work for me, as my page bounced slightly by clicking on my scroll animation.
I decided to update the entire URL using window.history.replaceState rather than using the window.location.hash method. Thus, bypassing the hashChange event triggered by the browser.
// Only fire when URL has anchor $('a[href*="#"]:not([href="#"])').on('click', function(event) { // Prevent default anchor handling (which causes the page-jumping) event.preventDefault(); 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 ) { // Smooth scrolling to anchor $('html, body').animate({ scrollTop: target.offset().top }, 1000); // Update URL window.history.replaceState("", document.title, window.location.href.replace(location.hash, "") + this.hash); } } });
Swen Mar 29 '17 at 23:59 on 2017-03-29 23:59
source share