Anchor锚点

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

何时使用

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

代码演示

Anchor锚点 - 图1

基本

最简单的用法。

TypeScript

JavaScript

  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 事件

点击锚点不记录历史。

TypeScript

JavaScript

  1. import { Anchor } from 'antd';
  2. const { Link } = Anchor;
  3. const handleClick = (
  4. e: React.MouseEvent<HTMLElement>,
  5. link: {
  6. title: React.ReactNode;
  7. href: string;
  8. },
  9. ) => {
  10. e.preventDefault();
  11. console.log(link);
  12. };
  13. ReactDOM.render(
  14. <Anchor affix={false} onClick={handleClick}>
  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. mountNode,
  23. );

Anchor锚点 - 图3

设置锚点滚动偏移量

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

TypeScript

JavaScript

  1. import React, { useState, useEffect } from 'react';
  2. import { Anchor } from 'antd';
  3. const { Link } = Anchor;
  4. const AnchorExample: React.FC = () => {
  5. const [targetOffset, setTargetOffset] = useState<number | undefined>(undefined);
  6. useEffect(() => {
  7. setTargetOffset(window.innerHeight / 2);
  8. }, []);
  9. return (
  10. <Anchor targetOffset={targetOffset}>
  11. <Link href="#components-anchor-demo-basic" title="Basic demo" />
  12. <Link href="#components-anchor-demo-static" title="Static demo" />
  13. <Link href="#API" title="API">
  14. <Link href="#Anchor-Props" title="Anchor Props" />
  15. <Link href="#Link-Props" title="Link Props" />
  16. </Link>
  17. </Anchor>
  18. );
  19. };
  20. ReactDOM.render(<AnchorExample />, mountNode);

Anchor锚点 - 图4

静态位置

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

TypeScript

JavaScript

  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

自定义锚点高亮

自定义锚点高亮。

TypeScript

JavaScript

  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

监听锚点链接改变

监听锚点链接改变

TypeScript

JavaScript

  1. import { Anchor } from 'antd';
  2. const { Link } = Anchor;
  3. const onChange = (link: string) => {
  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() => window
offsetBottom距离窗口底部达到指定偏移量后触发number
offsetTop距离窗口顶部达到指定偏移量后触发number
showInkInFixed固定模式是否显示小圆点booleanfalse
onClickclick 事件的 handlerFunction(e: Event, link: Object)-
getCurrentAnchor自定义高亮的锚点() => string-
targetOffset锚点滚动偏移量,默认与 offsetTop 相同,例子numberoffsetTop
onChange监听锚点链接改变(currentActiveLink: string) => void

Link Props

成员说明类型默认值版本
href锚点链接string
title文字内容string|ReactNode
target该属性指定在何处显示链接的资源。string