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 lang="ts">
  12. import { defineComponent } from 'vue';
  13. export default defineComponent({
  14. setup() {
  15. const handleClick = (e: Event, link: string) => {
  16. e.preventDefault();
  17. console.log(link);
  18. };
  19. return {
  20. handleClick,
  21. };
  22. },
  23. });
  24. </script>

Anchor锚点 - 图4

设置锚点滚动偏移量

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

  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 lang="ts">
  12. import { defineComponent, onMounted, ref } from 'vue';
  13. export default defineComponent({
  14. setup() {
  15. const targetOffset = ref<number | undefined>(undefined);
  16. onMounted(() => {
  17. targetOffset.value = window.innerHeight / 2;
  18. });
  19. return {
  20. targetOffset,
  21. };
  22. },
  23. });
  24. </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" :getCurrentAnchor="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 lang="ts">
  12. import { defineComponent } from 'vue';
  13. export default defineComponent({
  14. setup() {
  15. const getCurrentAnchor = () => {
  16. return '#components-anchor-demo-static';
  17. };
  18. return {
  19. getCurrentAnchor,
  20. };
  21. },
  22. });
  23. </script>

Anchor锚点 - 图7

监听锚点链接改变

监听锚点链接改变。

  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 lang="ts">
  12. import { defineComponent } from 'vue';
  13. export default defineComponent({
  14. setup() {
  15. const onChange = (link: string) => {
  16. console.log('Anchor:OnChange', link);
  17. };
  18. return {
  19. onChange,
  20. };
  21. },
  22. });
  23. </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