alicelieutier / smoothScroll

A teeny tiny smooth scroll script with ease-in-out effect and no jQuery.
MIT License
537 stars 127 forks source link

preventDefault() on anchor click event does nothing #43

Closed fenok closed 6 years ago

fenok commented 7 years ago

Smoothscroll always changes location.hash. It should consider event.defaultPrevented. Costed me a lot of time while debugging complex project.

alicelieutier commented 6 years ago

Hi @fenok, thanks for your feedback. I am not sure what you mean by "it should consider event.defaultPrevented". Can you explain your problem a bit more?

fenok commented 6 years ago

Sure. I'm talking about this and this pieces of code. They change location.hash regardless of whether event.preventDefault() has been called on the click event. In my case, that breaks hash-based routing, which is used due to Cordova restrictions. Anchors (hashes) are translated into query params (like 'path/to/page#anchorname' => '#path/to/page?anchor=anchorname') by internal router, and scroll behaviour is scripted there. Therefore I would like to be able to prevent default browser behaviour (hash change and scroll), which is impossible with smoothScroll. This could be fixed by checking event.defaultPrevented before changing location.hash (and before scrolling, of course). This would also make smoothScroll consistent with native 'default preventable' links.

alicelieutier commented 6 years ago

Ok, this makes sense. Do you want to submit a PR for this?

fenok commented 6 years ago

That sounded like a good opportunity to learn how they works, so I did =)