EaseAnchor 是一个灵活的 React 锚点导航组件,提供了易用的页面内导航功能。它支持多级嵌套的锚点结构,可自定义滚动容器,以及样式,并提供平滑滚动和即时跳转两种模式。(目前还处于beta版本)
- 支持多级嵌套的锚点结构
- 可自定义滚动容器
- 支持平滑滚动和即时跳转
- 自动检测并高亮当前可见的锚点
- 可自定义锚点项的样式和类名
- 支持通过 URL hash 或默认值设置初始活动锚点
- 提供全局点击回调函数
npm install ease-anchor
import React from 'react';
import EaseAnchor from 'ease-anchor';
const App = () => {
const items = [
{ href: 'section1', content: 'Section 1' },
{ href: 'section2', content: 'Section 2' },
{
href: 'section3',
content: 'Section 3',
children: [
{ href: 'section3-1', content: 'Section 3.1' },
{ href: 'section3-2', content: 'Section 3.2' },
],
},
];
return (
<div>
<EaseAnchor items={items} offset={60} />
<div id="section1">Section 1 content</div>
<div id="section2">Section 2 content</div>
<div id="section3">
Section 3 content
<div id="section3-1">Section 3.1 content</div>
<div id="section3-2">Section 3.2 content</div>
</div>
</div>
);
};
export default App;
属性 | 类型 | 默认值 | 描述 |
---|---|---|---|
items | AnchorItem[] | - | 锚点项数组,定义导航结构 |
scrollContainer | string | HTMLElement | window | 滚动容器的 ID 或 HTMLElement |
offset | number | 0 | 滚动偏移量 |
animation | boolean | true | 是否使用平滑滚动 |
onClick | (href: string, hierarchy: string[]) => void | - | 点击锚点时的回调函数 |
className | string | '' | 导航容器的自定义类名 |
style | CSSProperties | {} | 导航容器的自定义样式 |
itemClassName | string | '' | 锚点项的自定义类名 |
itemStyle | CSSProperties | {} | 锚点项的自定义样式 |
defaultValue | string | - | 默认激活的锚点 href |
AnchorItem 接口定义了每个锚点项的结构:
属性 | 类型 | 描述 |
---|---|---|
href | string | 锚点的目标 ID |
content | ReactNode | ((isActive: boolean, level: number) => ReactNode) | 锚点的显示内容 |
children | AnchorItem[] | 可选,子锚点项 |
你可以通过 scrollContainer
属性指定自定义的滚动容器:
<EaseAnchor items={items} scrollContainer="custom-container" />
您可以使用函数来动态生成锚点内容:
const items = [
{
href: 'dynamic',
content: (isActive, level) => (
<span style={{ color: isActive ? 'red' : 'black' }}>Dynamic Content (Level: {level})</span>
),
},
];
您可以使用 onClick 回调来执行自定义操作:
const handleClick = (href, hierarchy) => {
console.log(`Clicked: ${href}, Hierarchy: ${hierarchy.join(' > ')}`);
};
<EaseAnchor items={items} onClick={handleClick} />;
EaseAnchor 使用 MIT 许可证。查看 LICENSE 文件了解更多信息。
如果您有任何问题或建议,请通过以下方式联系我们:
- 提交 GitHub Issue
- 发送邮件到 [email protected]
感谢您使用 EaseAnchor!