Anchor 錨點
列出頁面裡的幾個章節,點了捲動到對應位置,捲動時醒目標示目前所在的章節。
引入
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
基礎用法
items 裡每一項的 href 寫成 #id,指向頁面上 id 相同的元素(也可以是 data-anchor-id 相同的元素)。點擊連結平滑捲動到那個元素;捲動時,已經滾過頂部的章節裡最靠下的那個會醒目標示。
不傳 container 時監聽頁面捲動。為了方便看效果,本頁的示例都放在固定高度的捲動容器裡,用 container 指向這個容器(用法見下面的“自訂捲動容器”)。點右邊的連結,或者在左邊的框裡捲動:
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-basic-1', label: '基本資訊' },
{ href: '#anchor-basic-2', label: '聯絡方式' },
{ href: '#anchor-basic-3', label: '賬號設定' }
]
</script>
<template>
<div class="layout">
<div id="anchor-basic-box" class="box">
<section id="anchor-basic-1" class="section">基本資訊</section>
<section id="anchor-basic-2" class="section">聯絡方式</section>
<section id="anchor-basic-3" class="section">賬號設定</section>
</div>
<HuiAnchor :items="items" container="#anchor-basic-box" />
</div>
</template>
<style scoped>
.layout {
display: flex;
gap: 24px;
}
.box {
flex: 1;
height: 240px;
overflow: auto;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.section {
height: 280px;
padding: 16px;
border-bottom: 1px dashed var(--hui-border-color);
color: var(--hui-text-color-secondary);
}
</style>
風格
type 可選 rail(預設,左邊有一條軌道,目前項旁邊有一段醒目的墨條)和 block(目前項顯示成帶淺色背景的塊)。rail 風格下 show-rail 設成 false 可以去掉軌道和墨條,只靠文字顏色標出目前項;block 風格下 show-rail 不起作用。
下面三個錨點監聽同一個捲動容器,捲動時一起變化:
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-type-1', label: '基本資訊' },
{ href: '#anchor-type-2', label: '聯絡方式' },
{ href: '#anchor-type-3', label: '賬號設定' }
]
</script>
<template>
<div class="layout">
<div id="anchor-type-box" class="box">
<section id="anchor-type-1" class="section">基本資訊</section>
<section id="anchor-type-2" class="section">聯絡方式</section>
<section id="anchor-type-3" class="section">賬號設定</section>
</div>
<div>
<p class="row-label">rail</p>
<HuiAnchor :items="items" type="rail" container="#anchor-type-box" />
</div>
<div>
<p class="row-label">rail,show-rail=false</p>
<HuiAnchor :items="items" :show-rail="false" container="#anchor-type-box" />
</div>
<div>
<p class="row-label">block</p>
<HuiAnchor :items="items" type="block" container="#anchor-type-box" />
</div>
</div>
</template>
<style scoped>
.layout {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
.box {
flex: 1;
min-width: 200px;
height: 240px;
overflow: auto;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.section {
height: 280px;
padding: 16px;
border-bottom: 1px dashed var(--hui-border-color);
color: var(--hui-text-color-secondary);
}
.row-label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
頂部偏移
捲動區域頂部有固定的導航欄時,用 target-offset 設定它的高度:點擊跳轉時目標停在離頂部這麼遠的位置,判斷醒目狀態時也按這個位置算。bound 是判斷醒目狀態時再額外放寬的距離,預設 10,章節標題滾到離偏移線不到這個距離時就算進入了這一節。
下面的框頂部有一條 40px 高、貼住不動的標題欄。左邊的錨點設了 :target-offset="40",點擊後章節標題露在標題欄下面;中間的沒設,章節標題被標題欄擋住;右邊設了 :bound="120",章節離頂部還有 160px 時就提前醒目標示。
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-offset-1', label: '基本資訊' },
{ href: '#anchor-offset-2', label: '聯絡方式' },
{ href: '#anchor-offset-3', label: '賬號設定' }
]
</script>
<template>
<div class="layout">
<div id="anchor-offset-box" class="box">
<div class="header">固定的標題欄(40px)</div>
<section id="anchor-offset-1" class="section">基本資訊</section>
<section id="anchor-offset-2" class="section">聯絡方式</section>
<section id="anchor-offset-3" class="section">賬號設定</section>
</div>
<div>
<p class="row-label">target-offset=40</p>
<HuiAnchor :items="items" :target-offset="40" container="#anchor-offset-box" />
</div>
<div>
<p class="row-label">不設</p>
<HuiAnchor :items="items" container="#anchor-offset-box" />
</div>
<div>
<p class="row-label">bound=120</p>
<HuiAnchor :items="items" :target-offset="40" :bound="120" container="#anchor-offset-box" />
</div>
</div>
</template>
<style scoped>
.layout {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
.box {
flex: 1;
min-width: 200px;
height: 240px;
overflow: auto;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.header {
position: sticky;
top: 0;
z-index: 1;
height: 40px;
padding: 0 16px;
line-height: 40px;
background-color: var(--hui-color-primary);
color: var(--hui-color-white);
}
.section {
height: 280px;
padding: 16px;
border-bottom: 1px dashed var(--hui-border-color);
color: var(--hui-text-color-secondary);
}
.row-label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
固定在側邊
affix 為 true 時錨點留在原來的位置,捲動時貼在捲動容器的頂部(離頂部 target-offset 像素),不會蓋住正文。它用的是 position: sticky,只在父元素的範圍內貼住,所以父元素要和正文一樣高,常見的寫法是和正文並排放在一個 flex 容器裡。
下面錨點和正文都在捲動容器裡,往下滾時錨點貼在離頂部 16px 處:
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-affix-1', label: '簡介' },
{ href: '#anchor-affix-2', label: '用法' },
{ href: '#anchor-affix-3', label: '常見問題' }
]
</script>
<template>
<div
id="anchor-affix-box"
style="height: 300px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<div style="display: flex; gap: 24px; padding: 0 16px">
<main style="flex: 1">
<section id="anchor-affix-1" style="height: 400px; padding-top: 16px">簡介</section>
<section id="anchor-affix-2" style="height: 400px; padding-top: 16px">用法</section>
<section id="anchor-affix-3" style="height: 400px; padding-top: 16px">常見問題</section>
</main>
<HuiAnchor :items="items" affix :target-offset="16" container="#anchor-affix-box" />
</div>
</div>
</template>
自訂捲動容器
內容在某個可以捲動的元素裡時,用 container 指定這個元素,可以傳 CSS 選擇器、元素或 window。選擇器在元件掛載時查找,找不到就用 window。
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#part-1', label: '第一部分' },
{ href: '#part-2', label: '第二部分' },
{ href: '#part-3', label: '第三部分' }
]
</script>
<template>
<div style="display: flex; gap: 24px">
<div
id="anchor-demo-box"
style="flex: 1; height: 300px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<section id="part-1" style="height: 400px">第一部分</section>
<section id="part-2" style="height: 400px">第二部分</section>
<section id="part-3" style="height: 400px">第三部分</section>
</div>
<HuiAnchor :items="items" container="#anchor-demo-box" />
</div>
</template>
自訂標題
label 除了字串還可以是 VNode,比如在文字前面加圖示。
<script setup lang="ts">
import { h } from 'vue'
import { HuiAnchor, HuiIcon } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-title-1', label: h('span', [h(HuiIcon, { name: 'user' }), ' 個人資料']) },
{ href: '#anchor-title-2', label: h('span', [h(HuiIcon, { name: 'setting' }), ' 偏好設定']) },
{ href: '#anchor-title-3', label: h('span', [h(HuiIcon, { name: 'bell' }), ' 消息通知']) }
]
</script>
<template>
<div style="display: flex; gap: 24px">
<div
id="anchor-title-box"
style="flex: 1; height: 240px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<section id="anchor-title-1" style="height: 280px; padding: 16px">個人資料</section>
<section id="anchor-title-2" style="height: 280px; padding: 16px">偏好設定</section>
<section id="anchor-title-3" style="height: 280px; padding: 16px">消息通知</section>
</div>
<HuiAnchor :items="items" container="#anchor-title-box" />
</div>
</template>
監聽醒目狀態變化
目前醒目標示的項目變化時觸發 change,參數是新的 href 和原來的 href。點擊連結時立即觸發一次,第一次醒目標示時原來的 href 是空字串。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-change-1', label: '基本資訊' },
{ href: '#anchor-change-2', label: '聯絡方式' },
{ href: '#anchor-change-3', label: '賬號設定' }
]
const current = ref('')
const prev = ref('')
const handleChange = (currentHref: string, context: { prevHref: string }) => {
current.value = currentHref
prev.value = context.prevHref
}
</script>
<template>
<p style="margin: 0 0 12px">目前章節:{{ current || '無' }},上一個:{{ prev || '無' }}</p>
<div style="display: flex; gap: 24px">
<div
id="anchor-change-box"
style="flex: 1; height: 240px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<section id="anchor-change-1" style="height: 280px; padding: 16px">基本資訊</section>
<section id="anchor-change-2" style="height: 280px; padding: 16px">聯絡方式</section>
<section id="anchor-change-3" style="height: 280px; padding: 16px">賬號設定</section>
</div>
<HuiAnchor :items="items" container="#anchor-change-box" @change="handleChange" />
</div>
</template>
用程式碼捲動
透過 ref 呼叫 scrollToAnchor(href),可以在程式碼裡捲動到某個章節,效果和點連結時的捲動一樣。ref 上還能讀到目前醒目標示的 currentHref。
<script setup lang="ts">
import { ref } from 'vue'
import type { AnchorExpose } from '@nscode/handy-ui'
import { HuiAnchor, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const anchorRef = ref<AnchorExpose>()
const items: AnchorItem[] = [
{ href: '#anchor-method-1', label: '基本資訊' },
{ href: '#anchor-method-2', label: '聯絡方式' },
{ href: '#anchor-method-3', label: '賬號設定' }
]
</script>
<template>
<HuiSpace wrap style="margin-bottom: 12px">
<HuiButton @click="anchorRef?.scrollToAnchor('#anchor-method-1')">跳到基本資訊</HuiButton>
<HuiButton @click="anchorRef?.scrollToAnchor('#anchor-method-2')">跳到聯絡方式</HuiButton>
<HuiButton @click="anchorRef?.scrollToAnchor('#anchor-method-3')">跳到賬號設定</HuiButton>
<span>currentHref:{{ anchorRef?.currentHref || '無' }}</span>
</HuiSpace>
<div style="display: flex; gap: 24px">
<div
id="anchor-method-box"
style="flex: 1; height: 240px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<section id="anchor-method-1" style="height: 280px; padding: 16px">基本資訊</section>
<section id="anchor-method-2" style="height: 280px; padding: 16px">聯絡方式</section>
<section id="anchor-method-3" style="height: 280px; padding: 16px">賬號設定</section>
</div>
<HuiAnchor ref="anchorRef" :items="items" container="#anchor-method-box" />
</div>
</template>
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| items | AnchorItem[] | [] | 錨點清單 |
| targetOffset | number | 0 | 跳轉後目標離捲動容器頂部的距離(px),判斷醒目狀態時也按它算;affix 時也是貼住的位置 |
| container | string | HTMLElement | Window | - | 捲動容器,字串按 CSS 選擇器查找。不傳時掛載後用 window,選擇器找不到時也用 window |
| bound | number | 10 | 判斷醒目狀態時額外加的距離(px):章節頂部滾到離頂部 targetOffset + bound 以內就算進入這一節 |
| affix | boolean | false | 是否在捲動時貼在捲動容器頂部(position: sticky) |
| type | 'block' | 'rail' | 'rail' | 風格 |
| showRail | boolean | true | rail 風格下是否顯示左邊的軌道和墨條,block 風格下不起作用 |
AnchorItem
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| href | string | - | 必填。寫成 #id,對應頁面上 id 或 data-anchor-id 相同的元素 |
| label | string | VNode | - | 必填。連結顯示的內容 |
| target | '_self' | '_blank' | - | 寫到連結的 target 屬性上。點擊會被元件攔截改成頁面內捲動,對點擊沒有影響 |
Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| change | (currentHref: string, context: { prevHref: string }) | 醒目標示的項目變化時觸發。點擊連結時立即觸發一次;變成沒有醒目標示項目時不觸發。第一次醒目標示時 prevHref 是 '' |
暴露的屬性和方法
透過元件的 ref 訪問:
| 名稱 | 類型 | 說明 |
|---|---|---|
| currentHref | string | 目前醒目標示的項目的 href,沒有醒目標示項目時是 '' |
| inkVisible | boolean | rail 風格的墨條是否顯示,有醒目標示項目時為 true |
| scrollToAnchor | (href: string) => void | 平滑捲動到 href 對應的元素,找不到元素時什麼都不做 |
類型
import type {
AnchorProps,
AnchorItem,
AnchorTarget,
AnchorType,
AnchorEmits,
AnchorChangeContext,
AnchorExpose
} from '@nscode/handy-ui'
import type { VNode } from 'vue'
type AnchorTarget = string | HTMLElement | Window
type AnchorType = 'block' | 'rail'
interface AnchorItem {
href: string
label: string | VNode
target?: '_self' | '_blank'
}
interface AnchorProps {
items?: AnchorItem[]
targetOffset?: number
container?: AnchorTarget
bound?: number
affix?: boolean
type?: AnchorType
showRail?: boolean
}
interface AnchorEmits {
change: [currentHref: string, context: AnchorChangeContext]
}
interface AnchorChangeContext {
prevHref: string
}
interface AnchorExpose {
currentHref: string
inkVisible: boolean
scrollToAnchor: (href: string) => void
}