Anchor 锚点

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

何时使用

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

代码演示

基本

最简单的用法。

Anchor锚点 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Anchor } from 'choerodon-ui';
  4. const { Link } = Anchor;
  5. ReactDOM.render(
  6. <Anchor>
  7. <Link href="#components-anchor-demo-basic" title="Basic demo" />
  8. <Link href="#components-anchor-demo-static" title="Static demo" />
  9. <Link href="#API" title="API">
  10. <Link href="#Anchor-Props" title="Anchor Props" />
  11. <Link href="#Link-Props" title="Link Props" />
  12. </Link>
  13. </Anchor>,
  14. document.getElementById('container'),
  15. );

静态位置

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

Anchor锚点 - 图2

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

自定义 onClick 事件

点击锚点不记录历史。

Anchor锚点 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Anchor } from 'choerodon-ui';
  4. const { Link } = Anchor;
  5. const handleClick = (e, link) => {
  6. e.preventDefault();
  7. console.log(link);
  8. };
  9. ReactDOM.render(
  10. <Anchor affix={false} onClick={handleClick}>
  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. document.getElementById('container'),
  19. );

自定义锚点高亮

自定义锚点高亮。

Anchor锚点 - 图4

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

设置锚点滚动偏移量

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

Anchor锚点 - 图5

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

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

Link Props

成员说明类型默认值
href锚点链接string
title文字内容string|ReactNode