0

I'm trying to get smooth scrolling between elements to work properly, but if the hash links are enabled then it starts to jump on top and ignore offset. I can't figure out how to fix it. Maybe someone can help?

$('a[href^="#"]')
    .not('[href$="#"]')
    .not('[href$="#0"]')
    .on("click", function() {

    var $offset = $("#nav").height();
    var target = this.hash;

    $("html, body").stop().animate({
    scrollTop: $(target).offset().top - $offset
  }, 1000, function() {
    window.location.hash = target; // Enable hash links
  });

    return false;
});

Demo: https://jsfiddle.net/Lugjqk24/

Thank you.

mrWilson
  • 91
  • 1
  • 2
  • 13

1 Answers1

0

Your anchors are set at the top of the link, not at the top of the section element.

You can either change the anchor target. Or update the page url without navigating.

How do I modify the URL without reloading the page?

John
  • 5,942
  • 3
  • 42
  • 79