
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
scroll-to-fragment
Advanced tools
Make single page apps scroll according to the current URL hash.
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.
npm install scroll-to-fragment
In your app's initialization code, for example in index.js
:
import { scrollToFragment } from "scroll-to-fragment";
scrollToFragment();
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" }),
});
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.
If you want to remove all listeners and observers set up by scrollToFragment()
, you can call:
import { stopScrollToFragment } from "scroll-to-fragment";
stopScrollToFragment();
popstate
or hashchange
may result in unwanted scrolling after browser back and forward navigation.npm run watch
FAQs
Make single page apps scroll according to the current URL hash.
The npm package scroll-to-fragment receives a total of 4,608 weekly downloads. As such, scroll-to-fragment popularity was classified as popular.
We found that scroll-to-fragment demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.