scroll-to-fragment
v1.0.12
Published
Make single page apps scroll according to the current URL hash.
Downloads
19,157
Readme
Scroll to fragment
Make single page apps scroll to the current URL hash.
When you follow a link that ends with a hash fragment (https://example.com#my-headline) on a traditional server-side-rendered (SSR) website, the browser automatically scrolls to the matching element. With JavaScript-driven single page apps (SPA), current browsers do not scroll by default.
This helper provides single page apps with the classic scrolling behavior. It updates the scroll position on load, and checks for updates on clicks and browser history changes. To keep the fragment in line with asynchronously updated content (for example in a ReactJS based app) it also adjusts the scroll position on DOM changes.
Installation
npm install scroll-to-fragment
Usage
In your app's initialization code, for example in index.js
:
import { scrollToFragment } from "scroll-to-fragment";
scrollToFragment();
Options
You can customize the behavior with the following options:
import { scrollToFragment } from "scroll-to-fragment";
import { createBrowserHistory } from "history";
scrollToFragment({
// customize the target of a given fragment ID (default is getElementById):
getElement: (fragmentId) => document.getElementsByName(fragmentId)[0],
// adjust the scroll position after history PUSH events:
history: createBrowserHistory(),
// customize scrolling behavior:
scrollIntoView: (element) => element.scrollIntoView({ behavior: "smooth" }),
});
Manual trigger
If you are listening for any other events to trigger a scroll position update, simply call scrollToFragment()
again. This will automatically stop the previous instance.
Tips
- Triggering an update on
popstate
orhashchange
may result in unwanted scrolling after browser back and forward navigation. - If the scroll position after navigating back is wrong, we recommend using a dedicated package like delayed-scroll-restoration-polyfill.
Development
npm run watch