bendc / anchor-scroll

Smooth and lightweight anchor scrolling library
MIT License
363 stars 23 forks source link

Breaks browser's back button #22

Open stueja opened 8 years ago

stueja commented 8 years ago

I found out that scrolling to an anchor works wonderfully, however breaking the browser's back button's functionality (Chromium, Firefox, Safari Mobile). After commenting out the below, the back button works, however not scrolling smoothly.

I was trying onpopstate and different onpushstate variants to have it scroll smoothly to the top again, upon clicking the back button, but wasn't able to do so. If you have an idea, I'd appreciate it.

  const complete = (hash, coordinates) => {
    //history.pushState(null, null, hash);
    root.scrollTop = coordinates.get("start") + coordinates.get("delta");
  };

  const attachHandler = (links, index) => {
    const link = links.item(index);
    link.addEventListener("click", event => {
      //event.preventDefault();

      scroll(link);
    });
GabrielPerry commented 6 years ago

Hello... I'm experiencing the same issue and trying to figure out a solution. I'm bummed no one's responded to you on this. I'm still plugging away at it. If I come up with a solution, I'll post it here.

dziku86 commented 4 years ago

That's expected behavior when you preventDefault() on anchor links. Use history.pushState() to update url.