Skip to content
API 风格:

Anchor ​

In-page anchor navigation with scroll-linked highlighting. <nav> semantics plus aria-current="location".

Basic usage ​

vue
<script setup>
const anchorItems = [
  { id: 'sec-1', title: 'Section 1' },
  { id: 'sec-2', title: 'Section 2', children: [{ id: 'sec-2-1', title: 'Section 2.1' }] },
]
</script>

<template>
  <LhAnchor :items="anchorItems" />
</template>
vue
<script>
export default {
  data() {
    return {
      anchorItems: [
        { id: 'sec-1', title: 'Section 1' },
        { id: 'sec-2', title: 'Section 2', children: [{ id: 'sec-2-1', title: 'Section 2.1' }] },
      ],
    }
  },
}
</script>

<template>
  <LhAnchor :items="anchorItems" />
</template>

Horizontal layout and scroll options ​

direction="horizontal" lays the links out in a row; smooth disables smooth scrolling; offset compensates for the anchor region's offset from the top of the scroll container; target observes a specific scroll container (window by default).

vue
<script setup>
const anchorItems = [
  { id: 'sec-1', title: 'Section 1' },
  { id: 'sec-2', title: 'Section 2' },
]
</script>

<template>
  <!-- Horizontal anchors -->
  <LhAnchor
    :items="anchorItems"
    direction="horizontal"
    :offset="8"
    :smooth="true"
  />
</template>
vue
<script>
export default {
  data() {
    return {
      anchorItems: [
        { id: 'sec-1', title: 'Section 1' },
        { id: 'sec-2', title: 'Section 2' },
      ],
    }
  },
}
</script>

<template>
  <!-- Horizontal anchors -->
  <LhAnchor
    :items="anchorItems"
    direction="horizontal"
    :offset="8"
    :smooth="true"
  />
</template>

API ​

LhAnchor Props ​

AttributeDescriptionTypeDefault
itemsAnchor data (supports children)[LhAnchorItem](#type-LhAnchorItem)[]—
targetScroll container (selector or element; window by default)string | HTMLElement—
offsetOffset used when resolving the active anchornumber0
directionLayout direction"vertical" | "horizontal"'vertical'
smoothSmooth scrollingbooleantrue
sxQuick layout propsLhSxProps—

LhAnchor Events ​

EventDescriptionCallback
changeActive anchor changed (scroll-linked updates and anchor clicks)(id: string) => void
clickAn anchor link is clicked, before the scroll starts(e: MouseEvent, id: string) => void

LhAnchorItem ​

FieldDescriptionType
idTarget element idstring
titleDisplay textstring
childrenNested anchorsLhAnchorItem[]

Accessibility ​

  • The container is a semantic <nav> with an aria-label; the current anchor carries aria-current="location"
  • Links are real <a href="#id"> (works without JS; click is enhanced to smooth scrolling)
  • Smooth scrolling degrades to instant jumps under prefers-reduced-motion: reduce

Type Definitions ​

The TypeScript type structures exported by this component (auto-extracted from packages/components/anchor/src/types.ts and @lh-ui/share/core/* sources; click each entry to expand; see the Props table above for attribute descriptions):

LhAnchorProps
ts
export interface LhAnchorProps {
  items: LhAnchorItem[]
  target?: string | HTMLElement
  offset?: number
  direction?: 'vertical' | 'horizontal'
  smooth?: boolean
  sx?: LhSxProps
}
LhFlatAnchorItem (from @lh-ui/share/core/anchor)
ts
export interface LhFlatAnchorItem {
  id: string
  title: string
  level: number
}
LhAnchorItem (from @lh-ui/share/core/anchor)
ts
export interface LhAnchorItem {
  id: string
  title: string
  children?: LhAnchorItem[]
}

Beauty of tradition, foundation of code