anchor-scroll icon indicating copy to clipboard operation
anchor-scroll copied to clipboard

Breaks browser's back button

Open stueja opened this issue 9 years ago • 2 comments

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);
    });

stueja avatar Oct 04 '16 18:10 stueja

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.

GabrielPerry avatar Jun 07 '18 04:06 GabrielPerry

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

dziku86 avatar Apr 22 '20 19:04 dziku86