https://github.com/scrivito/scroll-to-fragment
#️⃣Make single page apps scroll according to the current URL hash.
https://github.com/scrivito/scroll-to-fragment
anchor fragment hash link observe scroll spa
Last synced: 5 months ago
JSON representation
#️⃣Make single page apps scroll according to the current URL hash.
- Host: GitHub
- URL: https://github.com/scrivito/scroll-to-fragment
- Owner: Scrivito
- License: mit
- Created: 2020-03-04T17:01:57.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-10T19:22:25.000Z (6 months ago)
- Last Synced: 2024-10-30T04:55:36.953Z (6 months ago)
- Topics: anchor, fragment, hash, link, observe, scroll, spa
- Language: TypeScript
- Homepage:
- Size: 1.35 MB
- Stars: 7
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Scroll to fragment
[](https://travis-ci.org/github/Scrivito/scroll-to-fragment)
[](https://bundlephobia.com/result?p=scroll-to-fragment)
[](https://www.npmjs.com/package/scroll-to-fragment)
[](https://www.npmjs.com/package/scroll-to-fragment)
[](https://www.scrivito.com/?utm_source=npm&utm_medium=natural&utm_campaign=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
```sh
npm install scroll-to-fragment
```## Usage
In your app's initialization code, for example in `index.js`:
```js
import { scrollToFragment } from "scroll-to-fragment";scrollToFragment();
```### Options
You can customize the behavior with the following options:
```js
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` or `hashchange` 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](https://github.com/janpaul123/delayed-scroll-restoration-polyfill).## Development
```sh
npm run watch
```