0

I am using this script to scroll to the element ID

$('.smoothscroll').on('click',function(e) {
    if (/#/.test(this.href)) {
        if (document.location.pathname == "/") {        
            e.preventDefault();
            var offset = headerHeight;
            var target = this.hash;
            $('html, body').stop().animate({
                'scrollTop': $(target).offset().top - offset
            }, 500, 'swing', function() {
                // window.location.hash = target;
            });
        };
    };                
}); 

but if I uncomment window.location.hash = target; the page jumps after the scroll animation.

How can I scroll to the element and update the URL without the jump?

Junky
  • 958
  • 7
  • 17
  • why two nested `if` statements? You have the `&&`. Or rather use falsiness with `||` and an early `return;` – Roko C. Buljan Sep 08 '21 at 23:08
  • With `window.location.hash = target` you change the site. Does this answer your question? [How do I modify the URL without reloading the page?](https://stackoverflow.com/questions/824349/how-do-i-modify-the-url-without-reloading-the-page) – Sven Eberth Sep 08 '21 at 23:17

0 Answers0