Anchor锚点

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

何时使用#

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

开发者注意事项:

4.24.0 起,由于组件从 class 重构成 FC,之前一些获取 ref 并调用内部实例方法的写法都会失效

代码演示

最简单的用法。

expand codeexpand code
import { Anchor } from 'antd';
import React from 'react';

const { Link } = Anchor;

const App: React.FC = () => (
  <Anchor>
    <Link href="#components-anchor-demo-basic" title="Basic demo" />
    <Link href="#components-anchor-demo-static" title="Static demo" />
    <Link href="#API" title="API">
      <Link href="#Anchor-Props" title="Anchor Props" />
      <Link href="#Link-Props" title="Link Props" />
    </Link>
  </Anchor>
);

export default App;

点击锚点不记录历史。

expand codeexpand code
import { Anchor } from 'antd';
import React from 'react';

const { Link } = Anchor;

const handleClick = (
  e: React.MouseEvent<HTMLElement>,
  link: {
    title: React.ReactNode;
    href: string;
  },
) => {
  e.preventDefault();
  console.log(link);
};

const App: React.FC = () => (
  <Anchor affix={false} onClick={handleClick}>
    <Link href="#components-anchor-demo-basic" title="Basic demo" />
    <Link href="#components-anchor-demo-static" title="Static demo" />
    <Link href="#API" title="API">
      <Link href="#Anchor-Props" title="Anchor Props" />
      <Link href="#Link-Props" title="Link Props" />
    </Link>
  </Anchor>
);

export default App;

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

expand codeexpand code
import { Anchor } from 'antd';
import React, { useEffect, useState } from 'react';

const { Link } = Anchor;

const App: React.FC = () => {
  const [targetOffset, setTargetOffset] = useState<number | undefined>(undefined);

  useEffect(() => {
    setTargetOffset(window.innerHeight / 2);
  }, []);

  return (
    <Anchor targetOffset={targetOffset}>
      <Link href="#components-anchor-demo-basic" title="Basic demo" />
      <Link href="#components-anchor-demo-static" title="Static demo" />
      <Link href="#API" title="API">
        <Link href="#Anchor-Props" title="Anchor Props" />
        <Link href="#Link-Props" title="Link Props" />
      </Link>
    </Anchor>
  );
};

export default App;

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

expand codeexpand code
import { Anchor } from 'antd';
import React from 'react';

const { Link } = Anchor;

const App: React.FC = () => (
  <Anchor affix={false}>
    <Link href="#components-anchor-demo-basic" title="Basic demo" />
    <Link href="#components-anchor-demo-static" title="Static demo" />
    <Link href="#API" title="API">
      <Link href="#Anchor-Props" title="Anchor Props" />
      <Link href="#Link-Props" title="Link Props" />
    </Link>
  </Anchor>
);

export default App;

自定义锚点高亮。

expand codeexpand code
import { Anchor } from 'antd';
import React from 'react';

const { Link } = Anchor;

const getCurrentAnchor = () => '#components-anchor-demo-static';

const App: React.FC = () => (
  <Anchor affix={false} getCurrentAnchor={getCurrentAnchor}>
    <Link href="#components-anchor-demo-basic" title="Basic demo" />
    <Link href="#components-anchor-demo-static" title="Static demo" />
    <Link href="#API" title="API">
      <Link href="#Anchor-Props" title="Anchor Props" />
      <Link href="#Link-Props" title="Link Props" />
    </Link>
  </Anchor>
);

export default App;

监听锚点链接改变

expand codeexpand code
import { Anchor } from 'antd';
import React from 'react';

const { Link } = Anchor;

const onChange = (link: string) => {
  console.log('Anchor:OnChange', link);
};

const App: React.FC = () => (
  <Anchor affix={false} onChange={onChange}>
    <Link href="#components-anchor-demo-basic" title="Basic demo" />
    <Link href="#components-anchor-demo-static" title="Static demo" />
    <Link href="#API" title="API">
      <Link href="#Anchor-Props" title="Anchor Props" />
      <Link href="#Link-Props" title="Link Props" />
    </Link>
  </Anchor>
);

export default App;

API#

Anchor Props#

成员说明类型默认值版本
affix固定模式booleantrue
bounds锚点区域边界number5
getContainer指定滚动的容器() => HTMLElement() => window
getCurrentAnchor自定义高亮的锚点(activeLink: string) => string-
offsetTop距离窗口顶部达到指定偏移量后触发number
showInkInFixedaffix={false} 时是否显示小圆点booleanfalse
targetOffset锚点滚动偏移量,默认与 offsetTop 相同,例子number-
onChange监听锚点链接改变(currentActiveLink: string) => void-
onClickclick 事件的 handlerfunction(e: Event, link: Object)-
成员说明类型默认值版本
href锚点链接string-
target该属性指定在何处显示链接的资源。string-
title文字内容ReactNode-
Spin加载中BackTop回到顶部