Preview Updated 2026-05-10

Anchor

Sidebar that lists page anchors explicitly, highlights the section in view as you scroll, and smooth-scrolls on click.

Basic usage

items is the anchor list; each entry is { href, label }. href looks like #section-id and corresponds to a <h2 id="section-id"> on the page. One level of children nesting is supported.

背景

概述

这是概述部分的第 1 段示例文本。

这是概述部分的第 2 段示例文本。

这是概述部分的第 3 段示例文本。

这是概述部分的第 4 段示例文本。

基础用法

这是基础用法部分的第 1 段示例文本。

这是基础用法部分的第 2 段示例文本。

这是基础用法部分的第 3 段示例文本。

这是基础用法部分的第 4 段示例文本。

API

这是 API 部分的第 1 段示例文本。

这是 API 部分的第 2 段示例文本。

这是 API 部分的第 3 段示例文本。

这是 API 部分的第 4 段示例文本。

<script setup lang="ts">
import { CfAnchor, type AnchorItem } from '@chufix-design/vue';

const items: AnchorItem[] = [
  { href: '#anchor-intro', label: '概述' },
  { href: '#anchor-usage', label: '基础用法' },
  { href: '#anchor-api', label: 'API' },
];
</script>

<template>
  <div style="display: grid; grid-template-columns: 1fr 160px; gap: 24px; max-height: 320px; overflow-y: auto; border: 1px solid var(--line-1); border-radius: 8px; padding: 16px;">
    <div>
      <h3 id="anchor-intro" style="margin-top: 0;">概述</h3>
      <p style="color: var(--fg-2);" v-for="i in 4" :key="`a-${i}`">这是概述部分的第 {{ i }} 段示例文本。</p>
      <h3 id="anchor-usage">基础用法</h3>
      <p style="color: var(--fg-2);" v-for="i in 4" :key="`b-${i}`">这是基础用法部分的第 {{ i }} 段示例文本。</p>
      <h3 id="anchor-api">API</h3>
      <p style="color: var(--fg-2);" v-for="i in 4" :key="`c-${i}`">这是 API 部分的第 {{ i }} 段示例文本。</p>
    </div>
    <CfAnchor :items="items" :offset-top="0" />
  </div>
</template>
<CfAnchor items={items} offsetTop={0} />

API

PropTypeDefaultDescription
itemsAnchorItem[]Anchor list
offsetTopnumber0Top offset (in pixels) used for highlight detection and scroll-to
boundsnumber5An element is considered in view when its top distance ≤ bounds + offsetTop; smaller is stricter
targetstringScroll container selector; omit to listen on window

AnchorItem

FieldTypeDescription
hrefstringAnchor hash, e.g. #install
labelstringDisplay text
childrenAnchorItem[]Child anchors

Events: onChange(href) — fires when the highlighted anchor changes.

反馈与讨论

Anchor · Discussion

0
0 / 600
一键发送
正在加载评论...