Anchor锚点 - 图1

Anchor 锚点

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

何时使用

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

代码演示

Anchor锚点 - 图2

基本

最简单的用法。

  1. <template>
  2. <a-anchor>
  3. <a-anchor-link href="#components-anchor-demo-basic" title="Basic demo" />
  4. <a-anchor-link href="#components-anchor-demo-static" title="Static demo" />
  5. <a-anchor-link
  6. href="#components-anchor-demo-basic"
  7. title="Basic demo with Target"
  8. target="_blank"
  9. />
  10. <a-anchor-link href="#API" title="API">
  11. <a-anchor-link href="#Anchor-Props" title="Anchor Props" />
  12. <a-anchor-link href="#Link-Props" title="Link Props" />
  13. </a-anchor-link>
  14. </a-anchor>
  15. </template>

Anchor锚点 - 图3

自定义 click 事件

点击锚点不记录历史。

  1. <template>
  2. <a-anchor :affix="false" @click="handleClick">
  3. <a-anchor-link href="#components-anchor-demo-basic" title="Basic demo" />
  4. <a-anchor-link href="#components-anchor-demo-static" title="Static demo" />
  5. <a-anchor-link href="#API" title="API">
  6. <a-anchor-link href="#Anchor-Props" title="Anchor Props" />
  7. <a-anchor-link href="#Link-Props" title="Link Props" />
  8. </a-anchor-link>
  9. </a-anchor>
  10. </template>
  11. <script>
  12. export default {
  13. methods: {
  14. handleClick(e, link) {
  15. e.preventDefault();
  16. console.log(link);
  17. },
  18. },
  19. };
  20. </script>

Anchor锚点 - 图4

监听锚点链接改变

监听锚点链接改变

  1. <template>
  2. <a-anchor :affix="false" @change="onChange">
  3. <a-anchor-link href="#components-anchor-demo-basic" title="Basic demo" />
  4. <a-anchor-link href="#components-anchor-demo-static" title="Static demo" />
  5. <a-anchor-link href="#API" title="API">
  6. <a-anchor-link href="#Anchor-Props" title="Anchor Props" />
  7. <a-anchor-link href="#Link-Props" title="Link Props" />
  8. </a-anchor-link>
  9. </a-anchor>
  10. </template>
  11. <script>
  12. export default {
  13. methods: {
  14. onChange(link) {
  15. console.log('Anchor:OnChange', link);
  16. },
  17. },
  18. };
  19. </script>

Anchor锚点 - 图5

静态位置

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

  1. <template>
  2. <a-anchor :affix="false">
  3. <a-anchor-link href="#components-anchor-demo-basic" title="Basic demo" />
  4. <a-anchor-link href="#components-anchor-demo-static" title="Static demo" />
  5. <a-anchor-link href="#API" title="API">
  6. <a-anchor-link href="#Anchor-Props" title="Anchor Props" />
  7. <a-anchor-link href="#Link-Props" title="Link Props" />
  8. </a-anchor-link>
  9. </a-anchor>
  10. </template>

Anchor锚点 - 图6

自定义锚点高亮

自定义锚点高亮。

  1. <template>
  2. <a-anchor :affix="false" :get-current-anchor="getCurrentAnchor">
  3. <a-anchor-link href="#components-anchor-demo-basic" title="Basic demo" />
  4. <a-anchor-link href="#components-anchor-demo-static" title="Static demo" />
  5. <a-anchor-link href="#API" title="API">
  6. <a-anchor-link href="#Anchor-Props" title="Anchor Props" />
  7. <a-anchor-link href="#Link-Props" title="Link Props" />
  8. </a-anchor-link>
  9. </a-anchor>
  10. </template>
  11. <script>
  12. export default {
  13. methods: {
  14. getCurrentAnchor() {
  15. return '#components-anchor-demo-static';
  16. },
  17. },
  18. };
  19. </script>

Anchor锚点 - 图7

设置锚点滚动偏移量

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

  1. <template>
  2. <a-anchor :target-offset="targetOffset">
  3. <a-anchor-link href="#components-anchor-demo-basic" title="Basic demo" />
  4. <a-anchor-link href="#components-anchor-demo-static" title="Static demo" />
  5. <a-anchor-link href="#API" title="API">
  6. <a-anchor-link href="#Anchor-Props" title="Anchor Props" />
  7. <a-anchor-link href="#Link-Props" title="Link Props" />
  8. </a-anchor-link>
  9. </a-anchor>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. targetOffset: undefined,
  16. };
  17. },
  18. mounted() {
  19. this.targetOffset = window.innerHeight / 2;
  20. },
  21. };
  22. </script>

API

Anchor Props

成员说明类型默认值版本
affix固定模式booleantrue
bounds锚点区域边界number5(px)
getContainer指定滚动的容器() => HTMLElement() => window
offsetBottom距离窗口底部达到指定偏移量后触发number
offsetTop距离窗口顶部达到指定偏移量后触发number
showInkInFixed固定模式是否显示小圆点booleanfalse
wrapperClass容器的类名string-
wrapperStyle容器样式object-
getCurrentAnchor自定义高亮的锚点() => string-1.5.0
targetOffset锚点滚动偏移量,默认与 offsetTop 相同,例子numberoffsetTop1.5.0

事件

事件名称说明回调参数版本
clickclick 事件的 handlerFunction(e: Event, link: Object)
change监听锚点链接改变(currentActiveLink: string) => void

Link Props

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