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="#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 锚点 - 图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. 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. );

API

Anchor Props

成员说明类型默认值
affix固定模式booleantrue
bounds锚点区域边界number5(px)
getContainer指定滚动的容器() => HTMLElement() => window
offsetBottom距离窗口底部达到指定偏移量后触发number
offsetTop距离窗口顶部达到指定偏移量后触发number
showInkInFixed固定模式是否显示小圆点booleanfalse
onClickclick 事件的 handlerFunction(e: Event, link: Object)-
成员说明类型默认值
href锚点链接string
title文字内容string|ReactNode