@cascadia-code/scroll-to-hash-element

中文文档
2.0.3发布于 2024/10/04MITCommonJS零依赖近 30 天下载 1783

Detects location.hash changes and scrolls to the element with the corresponding id. Customizable scroll behavior.

原始使用文档
文档目录4 节
  1. Installation
  2. Usage
  3. Creating Hash Links
  4. Customize Scroll Behavior

Scroll-To-Hash-Element

npm GitHub License GitHub package.json dev/peer/optional dependency version

No longer requires react-router as dependency!

This component enables hash/anchor links to function within a React application.

It works by listening to the hash property of window.location and scrolling to the matching element if it exists.

This was originally written to solve the issue of hash links no longer working with React Router v6+, and required react-router as a dependency. However, it has been refactored to work with any router (or lack thereof).

Scrolling itself is provided by the native browser method Element.scrollIntoView()

Installation

Install the package with npm or yarn

npm install @cascadia-code/scroll-to-hash-element

Just place this component anywhere in the top level of the application and it will work passively in the background.

import React from "react";
import ScrollToHashElement from "@cascadia-code/scroll-to-hash-element";

import Header from "./Header";
import Content from "./Content";

const App = () => {
  return (
    <div className="grid">
      <ScrollToHashElement />
      <Header />
      <Content />
    </div>
  );
};

export default App;

Usage

With React-Router

You can create React-Router links as you normally would. For example a link to a hash element on the homepage would look like this

<Link to="/#some-div-id">Link Text</Link>

or this

<Link to="#some-div-id">Link Text</Link>

a sub page like this

<Link to="/about#story">Our Story</Link>

Without React-Router

You can create standard anchor tags. For example a link to a hash element on the homepage would look like this

<a href="/#some-div-id">Link Text</a>

or this

<a href="#some-div-id">Link Text</a>

a sub page like this

<a href="/about#story">Our Story</a>

Customize Scroll Behavior

You can customize the scroll behavior by passing props to the ScrollToHashElement component.

<ScrollToHashElement behavior="smooth" inline="center" block="center" />
propdefaultoptions
behavior"auto""auto", "instant", "smooth"
inline"nearest""center", "end", "nearest", "start"
block"start""center", "end", "nearest", "start"

You can read more about these properties here: Element.scrollIntoView()

版本与发布记录

@cascadia-code/scroll-to-hash-element 共发布 6 个版本,首次发布于 2024/05/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.3latest2024/10/042 年前
2.0.22024/06/022 年前
2.0.12024/06/022 年前
2.0.02024/06/022 年前
1.0.12024/05/312 年前
1.0.02024/05/312 年前

关于 @cascadia-code/scroll-to-hash-element 的常见问题

@cascadia-code/scroll-to-hash-element 是什么?

Detects location.hash changes and scrolls to the element with the corresponding id. Customizable scroll behavior.

如何安装 @cascadia-code/scroll-to-hash-element?

在项目目录运行 npm install @cascadia-code/scroll-to-hash-element,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.8.0 || ^17.0.0 || ^18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cascadia-code/scroll-to-hash-element 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cascadia-code__scroll-to-hash-element 是否存在。

@cascadia-code/scroll-to-hash-element 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cascadia-code/scroll-to-hash-element 可以商用吗?

@cascadia-code/scroll-to-hash-element 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cascadia-code/scroll-to-hash-element 的下载量有多少?

根据 npm 官方下载统计,@cascadia-code/scroll-to-hash-element 在 2026/10/09 – 2026/11/07 这 30 天内被下载 1,783 次,日均约 59 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cascadia-code/scroll-to-hash-element 还在维护吗?

最新版本 2.0.3 发布于 2024/10/04(2 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/05/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。