锚点
用于跳转到页面指定位置。
何时使用
需要展现当前页面上可供跳转的锚点链接,以及快速在锚点之间跳转。
开发者注意事项:
自
4.24.0起,由于组件从 class 重构成 FC,之前一些获取ref并调用内部实例方法的写法都会失效
代码演示
-
基本 ·
basic -
横向 Anchor ·
horizontal -
静态位置 ·
static -
自定义 onClick 事件 ·
onClick -
自定义锚点高亮 ·
customizeHighlight -
设置锚点滚动偏移量 ·
targetOffset -
监听锚点链接改变 ·
onChange -
替换历史中的 href ·
replace -
自定义语义结构的样式和类 ·
style-class
API
通用属性参考:通用属性
Anchor Props
| 参数 | 说明 | 类型 | 默认值 | 版本 | 全局配置 |
|---|---|---|---|---|---|
| affix | 固定模式 | boolean | Omit<AffixProps, 'offsetTop' | 'target' | 'children'> | true | object: 5.19.0 | × |
| bounds | 锚点区域边界 | number | 5 | × | |
| classNames | 用于自定义组件内部各语义化结构的 class,支持对象或函数 | Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string> | - | 6.0.0 | |
| getContainer | 指定滚动的容器 | () => HTMLElement | () => window | × | |
| getCurrentAnchor | 自定义高亮的锚点 | (activeLink: string) => string | - | × | |
| offsetTop | 距离窗口顶部达到指定偏移量后触发 | number | 0 | × | |
| showInkInFixed | affix={false} 时是否显示小方块 | boolean | false | × | |
| styles | 用于自定义组件内部各语义化结构的行内 style,支持对象或函数 | Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties> | - | 6.0.0 | |
| targetOffset | 锚点滚动偏移量,默认与 offsetTop 相同,例子 | number | - | × | |
| onChange | 监听锚点链接改变 | (currentActiveLink: string) => void | - | × | |
| onClick | click 事件的 handler | (e: MouseEvent, link: object) => void | - | × | |
| items | 数据化配置选项内容,支持通过 children 嵌套 | { key, href, title, target, children }[] 具体见 | - | 5.1.0 | × |
| direction | 设置导航方向 | vertical | horizontal | vertical | 5.2.0 | × |
| replace | 替换浏览器历史记录中项目的 href 而不是推送它 | boolean | false | 5.7.0 | × |
AnchorItem
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| key | 唯一标志 | string | number | - | |
| href | 锚点链接 | string | - | |
| target | 该属性指定在何处显示链接的资源 | string | - | |
| title | 文字内容 | ReactNode | - | |
| children | 嵌套的 Anchor Link,注意:水平方向该属性不支持 | AnchorItem[] | - | |
| replace | 替换浏览器历史记录中的项目 href 而不是推送它 | boolean | false | 5.7.0 |
| targetOffset | 设置单个锚点的滚动偏移量,会覆盖 Anchor 组件的 targetOffset 属性 | number | - | 6.4.0 |
Link Props
建议使用 items 形式。
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| href | 锚点链接 | string | - | |
| target | 该属性指定在何处显示链接的资源 | string | - | |
| title | 文字内容 | ReactNode | - | |
| targetOffset | 设置单个锚点的滚动偏移量,会覆盖 Anchor 组件的 targetOffset 属性 | number | - | 6.4.0 |
Semantic DOM
| 语义节点 | 原生说明 | 版本 |
|---|---|---|
root | 根元素,包含布局定位、内边距、边距、背景色等基础样式 | 6.0.0 |
item | 链接项元素,包含内边距、文字颜色、悬停状态、过渡动画等样式 | 6.0.0 |
itemTitle | 标题文字元素,包含字体样式、颜色变化、文本装饰、过渡效果等样式 | 6.0.0 |
indicator | 指示器元素,包含宽度、高度、背景色、位置变化、过渡动画等样式 | 6.0.0 |
语义说明来源: vendor/components/anchor/demo/_semantic.tsx
主题变量(Design Token)
类型、继承关系和原生令牌声明保持固定源码原文;默认值由原生主题计算,并可能受 ConfigProvider 覆盖。本表不伪造静态默认值。
| 令牌 | 类型 | 原生说明 |
|---|---|---|
linkPaddingBlock | number | 链接纵向内间距 |
linkPaddingInlineStart | number | 链接横向内间距 |
声明来源: vendor/components/anchor/style/index.ts
import type { CSSObject } from '@ant-design/cssinjs';
import { unit } from '@ant-design/cssinjs';
import { resetComponent, textEllipsis } from '../../style';
import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
import { genStyleHooks, mergeToken } from '../../theme/internal';
export interface ComponentToken {
/**
* @desc 链接纵向内间距
* @descEN Vertical padding of link
*/
linkPaddingBlock: number;
/**
* @desc 链接横向内间距
* @descEN Horizontal padding of link
*/
linkPaddingInlineStart: number;
}
/**
* @desc Anchor 组件的 Token
* @descEN Token for Anchor component
*/
interface AnchorToken extends FullToken<'Anchor'> {
/**
* @desc 容器块偏移量
* @descEN Holder block offset
*/
holderOffsetBlock: number;
/**
* @desc 次级锚点块内间距
* @descEN Secondary anchor block padding
*/
anchorPaddingBlockSecondary: number | string;
/**
* @desc 锚点球大小
* @descEN Anchor ball size
*/
anchorBallSize: number | string;
/**
* @desc 锚点标题块
* @descEN Anchor title block
*/
anchorTitleBlock: number | string;
}
FAQ
在 5.25.0+ 版本中,锚点跳转后,目标元素的 :target 伪类未按预期生效
出于页面性能优化考虑,锚点跳转的实现方式从 window.location.href 调整为 window.history.pushState/replaceState。由于 pushState/replaceState 不会触发页面重载,因此浏览器不会自动更新 :target 伪类的匹配状态。可以手动构造完整URL:href = window.location.origin + window.location.pathname + '#xxx' 来解决这问题。
原生通用属性
Tips: 以下通用属性适用于 antd 大部分组件,不支持的组件会单独说明。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| style | 自定义样式 | CSSProperties | - |
| className | 自定义类名 | string | - |
| rootClassName | 添加在组件最外层的 className | string | - |
| autoFocus | 自动获取焦点,仅对表单类、链接、交互容器等可聚焦元素生效 | boolean | false |
安装并复现示例
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23
使用 ConfigProvider 和原生 App,并单独配置 dayjs 语言。每个示例在查看代码中列出完整源码文件和额外固定依赖。示例名称用于选择演示,不是组件通用属性。主题、语言与 SSR 初始化见资源安装指南。
原生类型参考
源码查看器包含未修改的原生实现和以下声明文件,继承类型仍位于原生包中。代码中的英文注释保留为源码参考。
vendor/components/anchor/Anchor.tsxvendor/components/anchor/AnchorLink.tsxvendor/components/anchor/index.tsxvendor/components/anchor/style/index.ts
来源与许可
https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd
保留原生中英文正文与可执行示例;官网专用的嵌入章节和令牌表格在本地展开。Ant Design 使用 MIT 许可,另列依赖的许可证也需保留。上传预览禁止向外发起请求,不提供后端服务。