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

何时使用

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

案例:锚点的基本使用

核心代码:

<template>
  <a-anchor
    :items="[
      {
        key: 'part-1',
        href: '#part-1',
        title: () => h('span', { style: 'color: red' }, 'Part 1'),
      },
      {
        key: 'part-2',
        href: '#part-2',
        title: 'Part 2',
      },
      {
        key: 'part-3',
        href: '#part-3',
        title: 'Part 3',
      },
    ]"
  />
</template>
<script lang="ts" setup>
import { h } from 'vue';
</script>

vue3示例:

<script setup>
const menuItems = [
  {
    key: "index",
    href: "/",
    title: "首页"
  },
  {
    key: "auth",
    href: "/auth",
    title: "权限管理"
  },
  {
    key: "setting",
    href: "/setting",
    title: "配置管理"
  },
]
</script>
<template>
  <a-anchor :items="menuItems"/>
</template>

Python私教张大鹏 Vue3整合AntDesignVue之Anchor 锚点插图

案例:锚点的动态渲染

核心代码:

title: () => h('span', { style: 'color: red' }, 'Part 1'),

vue3示例:

<script setup>
import {h} from "vue";

const menuItems = [
  {
    key: "index",
    href: "/",
    title: ()=> h('span', {style: 'color: red'}, "首页")
  },
  {
    key: "auth",
    href: "/auth",
    title: "权限管理"
  },
  {
    key: "setting",
    href: "/setting",
    title: "配置管理"
  },
]
</script>
<template>
  <a-anchor :items="menuItems"/>
</template>

Python私教张大鹏 Vue3整合AntDesignVue之Anchor 锚点插图(1)

案例:横向锚点

设置 direction="horizontal" 能够实现横向锚点。

核心代码:

<template>
  <div
    style="
       {
        padding: '20px';
      }
    "
  >
    <a-anchor
      direction="horizontal"
      :items="[
        {
          key: 'horizontally-part-1',
          href: '#horizontally-part-1',
          title: 'Part 1',
        },
        {
          key: 'horizontally-part-2',
          href: '#horizontally-part-2',
          title: 'Part 2',
        },
        {
          key: 'horizontally-part-3',
          href: '#horizontally-part-3',
          title: 'Part 3',
        },
        {
          key: 'horizontally-part-4',
          href: '#horizontally-part-4',
          title: 'Part 4',
        },
        {
          key: 'horizontally-part-5',
          href: '#horizontally-part-5',
          title: 'Part 5',
        },
        {
          key: 'horizontally-part-6',
          href: '#horizontally-part-6',
          title: 'Part 6',
        },
      ]"
    />
  </div>
</template>

vue3示例:

<script setup>
import {h} from "vue";

const menuItems = [
  {
    key: "index",
    href: "/",
    title: ()=> h('span', {style: 'color: red'}, "首页")
  },
  {
    key: "auth",
    href: "/auth",
    title: "权限管理"
  },
  {
    key: "setting",
    href: "/setting",
    title: "配置管理"
  },
]
</script>
<template>
  <a-anchor :items="menuItems" direction="horizontal"/>
</template>

Python私教张大鹏 Vue3整合AntDesignVue之Anchor 锚点插图(2)

属性

成员说明类型默认值版本
affix固定模式booleantrue
bounds锚点区域边界number5(px)
getContainer指定滚动的容器() => HTMLElement() => window
getCurrentAnchor自定义高亮的锚点(activeLink: string) => stringactiveLink(3.3)
offsetBottom距离窗口底部达到指定偏移量后触发number
offsetTop距离窗口顶部达到指定偏移量后触发number
showInkInFixed:affix="false" 时是否显示小方块booleanfalse
targetOffset锚点滚动偏移量,默认与 offsetTop 相同,例子numberoffsetTop1.5.0
wrapperClass容器的类名string
wrapperStyle容器样式object
items数据化配置选项内容,支持通过 children 嵌套{ key, href, title, target, children }[] 具体见4.0
direction设置导航方向verticalhorizontalvertical
customTitle使用插槽自定义选项 titlev-slot=“AnchorItem”4.0

子节点属性

成员说明类型默认值版本
key唯一标志string | number
href锚点链接string
target该属性指定在何处显示链接的资源string
title文字内容VueNode | (item: AnchorItem) => VueNode
children嵌套的 Anchor Link,注意:水平方向该属性不支持AnchorItem[]

事件

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

链接属性

成员说明类型默认值版本
href锚点链接string
target该属性指定在何处显示链接的资源。string1.5.0
title文字内容string|slot
本站无任何商业行为
个人在线分享 » Python私教张大鹏 Vue3整合AntDesignVue之Anchor 锚点
E-->