2016-10-28 14:02:55 +08:00
|
|
|
|
---
|
|
|
|
|
category: Components
|
|
|
|
|
title: Anchor
|
2022-11-17 18:28:13 +08:00
|
|
|
|
subtitle: 锚点
|
2022-11-30 20:14:41 +08:00
|
|
|
|
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*TBTSR4PyVmkAAAAAAAAAAAAADrJ8AQ/original
|
2023-02-09 22:17:31 +08:00
|
|
|
|
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*JGb3RIzyOCkAAAAAAAAAAAAADrJ8AQ/original
|
2022-11-09 12:28:04 +08:00
|
|
|
|
demo:
|
2022-11-17 16:49:09 +08:00
|
|
|
|
group:
|
|
|
|
|
title: 导航
|
|
|
|
|
order: 3
|
2016-10-28 14:02:55 +08:00
|
|
|
|
---
|
|
|
|
|
|
2016-11-09 14:22:38 +08:00
|
|
|
|
用于跳转到页面指定位置。
|
2016-10-28 14:02:55 +08:00
|
|
|
|
|
|
|
|
|
## 何时使用
|
|
|
|
|
|
2016-11-03 16:45:13 +08:00
|
|
|
|
需要展现当前页面上可供跳转的锚点链接,以及快速在锚点之间跳转。
|
2016-10-28 14:02:55 +08:00
|
|
|
|
|
2022-10-31 14:49:43 +08:00
|
|
|
|
> 开发者注意事项:
|
|
|
|
|
>
|
|
|
|
|
> 自 `4.24.0` 起,由于组件从 class 重构成 FC,之前一些获取 `ref` 并调用内部实例方法的写法都会失效
|
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
|
## 代码演示
|
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
<!-- prettier-ignore -->
|
2022-12-12 19:44:32 +08:00
|
|
|
|
<code src="./demo/basic.tsx" iframe="200">基本</code>
|
2022-12-27 17:14:35 +08:00
|
|
|
|
<code src="./demo/horizontal.tsx" iframe="200">横向 Anchor</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/static.tsx">静态位置</code>
|
|
|
|
|
<code src="./demo/onClick.tsx">自定义 onClick 事件</code>
|
|
|
|
|
<code src="./demo/customizeHighlight.tsx">自定义锚点高亮</code>
|
2022-12-12 19:44:32 +08:00
|
|
|
|
<code src="./demo/targetOffset.tsx" iframe="200">设置锚点滚动偏移量</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/onChange.tsx">监听锚点链接改变</code>
|
2023-06-18 18:55:36 +08:00
|
|
|
|
<code src="./demo/replace.tsx" iframe="200">替换历史中的 href</code>
|
2022-11-30 15:55:43 +08:00
|
|
|
|
<code src="./demo/legacy-anchor.tsx" debug>废弃的 JSX 示例</code>
|
2023-07-31 18:26:33 +08:00
|
|
|
|
<code src="./demo/component-token.tsx" iframe="800" debug>组件 Token</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
2016-10-28 14:02:55 +08:00
|
|
|
|
## API
|
|
|
|
|
|
2023-08-08 18:27:48 +08:00
|
|
|
|
通用属性参考:[通用属性](/docs/react/common-props)
|
|
|
|
|
|
2016-11-03 16:45:13 +08:00
|
|
|
|
### Anchor Props
|
|
|
|
|
|
2023-05-07 17:28:33 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
2022-11-17 17:31:26 +08:00
|
|
|
|
| --- | --- | --- | --- | --- |
|
|
|
|
|
| affix | 固定模式 | boolean | true | |
|
|
|
|
|
| bounds | 锚点区域边界 | number | 5 | |
|
|
|
|
|
| getContainer | 指定滚动的容器 | () => HTMLElement | () => window | |
|
|
|
|
|
| getCurrentAnchor | 自定义高亮的锚点 | (activeLink: string) => string | - | |
|
|
|
|
|
| offsetTop | 距离窗口顶部达到指定偏移量后触发 | number | | |
|
2022-12-13 22:18:43 +08:00
|
|
|
|
| showInkInFixed | `affix={false}` 时是否显示小方块 | boolean | false | |
|
2022-12-22 14:12:26 +08:00
|
|
|
|
| targetOffset | 锚点滚动偏移量,默认与 offsetTop 相同,[例子](#components-anchor-demo-targetoffset) | number | - | |
|
2022-11-17 17:31:26 +08:00
|
|
|
|
| onChange | 监听锚点链接改变 | (currentActiveLink: string) => void | - | |
|
2022-12-06 18:18:38 +08:00
|
|
|
|
| onClick | `click` 事件的 handler | (e: MouseEvent, link: object) => void | - | |
|
2022-12-27 17:14:35 +08:00
|
|
|
|
| items | 数据化配置选项内容,支持通过 children 嵌套 | { key, href, title, target, children }\[] [具体见](#anchoritem) | - | 5.1.0 |
|
|
|
|
|
| direction | 设置导航方向 | `vertical` \| `horizontal` | `vertical` | 5.2.0 |
|
2023-06-18 18:55:36 +08:00
|
|
|
|
| replace | 替换浏览器历史记录中项目的 href 而不是推送它 | boolean | false | 5.7.0 |
|
2022-12-27 17:14:35 +08:00
|
|
|
|
|
|
|
|
|
### AnchorItem
|
|
|
|
|
|
2023-05-07 17:28:33 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
2022-12-27 17:14:35 +08:00
|
|
|
|
| --- | --- | --- | --- | --- |
|
|
|
|
|
| key | 唯一标志 | string \| number | - | |
|
|
|
|
|
| href | 锚点链接 | string | - | |
|
|
|
|
|
| target | 该属性指定在何处显示链接的资源 | string | - | |
|
|
|
|
|
| title | 文字内容 | ReactNode | - | |
|
|
|
|
|
| children | 嵌套的 Anchor Link,`注意:水平方向该属性不支持` | [AnchorItem](#anchoritem)\[] | - | |
|
2023-06-18 18:55:36 +08:00
|
|
|
|
| replace | 替换浏览器历史记录中的项目 href 而不是推送它 | boolean | false | 5.7.0 |
|
2016-11-07 11:04:36 +08:00
|
|
|
|
|
|
|
|
|
### Link Props
|
2016-10-28 14:02:55 +08:00
|
|
|
|
|
2022-12-27 17:14:35 +08:00
|
|
|
|
建议使用 items 形式。
|
|
|
|
|
|
2023-05-07 17:28:33 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
2022-12-27 17:14:35 +08:00
|
|
|
|
| ------ | ------------------------------ | --------- | ------ | ---- |
|
|
|
|
|
| href | 锚点链接 | string | - | |
|
|
|
|
|
| target | 该属性指定在何处显示链接的资源 | string | - | |
|
|
|
|
|
| title | 文字内容 | ReactNode | - | |
|
2023-03-27 15:33:22 +08:00
|
|
|
|
|
|
|
|
|
## Design Token
|
|
|
|
|
|
|
|
|
|
<ComponentTokenTable component="Anchor"></ComponentTokenTable>
|