Anchor锚点

用于跳转到页面指定位置。

何时使用

需要展现当前页面上可供跳转的锚点链接,以及快速在锚点之间跳转。

代码演示

Anchor锚点 - 图1

基本

最简单的用法。

  1. import { Anchor } from 'antd';
  2. const { Link } = Anchor;
  3. ReactDOM.render(
  4. <Anchor>
  5. <Link href="#components-anchor-demo-basic" title="Basic demo" />
  6. <Link href="#components-anchor-demo-static" title="Static demo" />
  7. <Link href="#components-anchor-demo-basic" title="Basic demo with Target" target="_blank" />
  8. <Link href="#API" title="API">
  9. <Link href="#Anchor-Props" title="Anchor Props" />
  10. <Link href="#Link-Props" title="Link Props" />
  11. </Link>
  12. </Anchor>,
  13. mountNode,
  14. );

Anchor锚点 - 图2

自定义 onClick 事件

点击锚点不记录历史。

  1. import { Anchor } from 'antd';
  2. const { Link } = Anchor;
  3. const handleClick = (e, link) => {
  4. e.preventDefault();
  5. console.log(link);
  6. };
  7. ReactDOM.render(
  8. <Anchor affix={false} onClick={handleClick}>
  9. <Link href="#components-anchor-demo-basic" title="Basic demo" />
  10. <Link href="#components-anchor-demo-static" title="Static demo" />
  11. <Link href="#API" title="API">
  12. <Link href="#Anchor-Props" title="Anchor Props" />
  13. <Link href="#Link-Props" title="Link Props" />
  14. </Link>
  15. </Anchor>,
  16. mountNode,
  17. );

Anchor锚点 - 图3

设置锚点滚动偏移量

锚点目标滚动到屏幕正中间。

  1. import { Anchor } from 'antd';
  2. const { Link } = Anchor;
  3. class AnchorExample extends React.Component {
  4. state = {
  5. targetOffset: undefined,
  6. };
  7. componentDidMount() {
  8. this.setState({
  9. targetOffset: window.innerHeight / 2,
  10. });
  11. }
  12. render() {
  13. return (
  14. <Anchor targetOffset={this.state.targetOffset}>
  15. <Link href="#components-anchor-demo-basic" title="Basic demo" />
  16. <Link href="#components-anchor-demo-static" title="Static demo" />
  17. <Link href="#API" title="API">
  18. <Link href="#Anchor-Props" title="Anchor Props" />
  19. <Link href="#Link-Props" title="Link Props" />
  20. </Link>
  21. </Anchor>
  22. );
  23. }
  24. }
  25. ReactDOM.render(<AnchorExample />, mountNode);

Anchor锚点 - 图4

静态位置

不浮动,状态不随页面滚动变化。

  1. import { Anchor } from 'antd';
  2. const { Link } = Anchor;
  3. ReactDOM.render(
  4. <Anchor affix={false}>
  5. <Link href="#components-anchor-demo-basic" title="Basic demo" />
  6. <Link href="#components-anchor-demo-static" title="Static demo" />
  7. <Link href="#API" title="API">
  8. <Link href="#Anchor-Props" title="Anchor Props" />
  9. <Link href="#Link-Props" title="Link Props" />
  10. </Link>
  11. </Anchor>,
  12. mountNode,
  13. );

Anchor锚点 - 图5

自定义锚点高亮

自定义锚点高亮。

  1. import { Anchor } from 'antd';
  2. const { Link } = Anchor;
  3. const getCurrentAnchor = () => {
  4. return '#components-anchor-demo-static';
  5. };
  6. ReactDOM.render(
  7. <Anchor affix={false} getCurrentAnchor={getCurrentAnchor}>
  8. <Link href="#components-anchor-demo-basic" title="Basic demo" />
  9. <Link href="#components-anchor-demo-static" title="Static demo" />
  10. <Link href="#API" title="API">
  11. <Link href="#Anchor-Props" title="Anchor Props" />
  12. <Link href="#Link-Props" title="Link Props" />
  13. </Link>
  14. </Anchor>,
  15. mountNode,
  16. );

Anchor锚点 - 图6

监听锚点链接改变

监听锚点链接改变

  1. import { Anchor } from 'antd';
  2. const { Link } = Anchor;
  3. const onChange = link => {
  4. console.log('Anchor:OnChange', link);
  5. };
  6. ReactDOM.render(
  7. <Anchor affix={false} onChange={onChange}>
  8. <Link href="#components-anchor-demo-basic" title="Basic demo" />
  9. <Link href="#components-anchor-demo-static" title="Static demo" />
  10. <Link href="#API" title="API">
  11. <Link href="#Anchor-Props" title="Anchor Props" />
  12. <Link href="#Link-Props" title="Link Props" />
  13. </Link>
  14. </Anchor>,
  15. mountNode,
  16. );

API

Anchor Props

成员说明类型默认值版本
affix固定模式booleantrue
bounds锚点区域边界number5(px)
getContainer指定滚动的容器() => HTMLElement() => window3.4.0
offsetBottom距离窗口底部达到指定偏移量后触发number
offsetTop距离窗口顶部达到指定偏移量后触发number
showInkInFixed固定模式是否显示小圆点booleanfalse
onClickclick 事件的 handlerFunction(e: Event, link: Object)-3.9.0
getCurrentAnchor自定义高亮的锚点() => string-3.22.0
targetOffset锚点滚动偏移量,默认与 offsetTop 相同,例子numberoffsetTop3.22.0
onChange监听锚点链接改变(currentActiveLink: string) => void3.24.0
成员说明类型默认值版本
href锚点链接string
title文字内容string|ReactNode
target该属性指定在何处显示链接的资源。string