Scrollbar 捲軸
包住需要捲動的內容,以外觀一致的細捲軸取代瀏覽器原生捲軸。捲軸採圓角、半透明與主題色,暗色主題下會自動調整為較淺色彩。
引入
import { HuiScrollbar } from '@nscode/handy-ui'
基礎用法
height 指定高度,內容超出時顯示捲軸。預設於滑鼠進入、捲動或拖曳時顯示,離開或停止操作 1 秒後淡出。可拖曳滑塊,亦可點擊軌道跳至該位置,按住後繼續拖曳。
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 行內容`)
</script>
<template>
<HuiScrollbar :height="200" class="box">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
水平捲動
內容超出區域寬度時顯示水平捲軸。內容容器預設為區塊元素,寬度等同區域;若內容水平排列,建議透過 view-style 設定 width: max-content,讓滑塊及時反映內容寬度。
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const cards = Array.from({ length: 20 }, (_, i) => i + 1)
const viewStyle = {
display: 'flex',
gap: '12px',
width: 'max-content',
padding: '12px 12px 16px'
}
</script>
<template>
<HuiScrollbar :view-style="viewStyle" class="box">
<div v-for="card in cards" :key="card" class="card">卡片 {{ card }}</div>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.card {
display: flex;
align-items: center;
justify-content: center;
width: 120px;
height: 80px;
border-radius: var(--hui-border-radius);
background: var(--hui-fill-color);
color: var(--hui-text-color-secondary);
}
</style>
最大高度
max-height 設定最大高度:內容不夠高時區域跟隨內容變矮,超過後出現捲軸。增刪內容時滑塊自動更新,不用手動處理。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
const count = ref(3)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton type="primary" icon="plus" @click="count++">加一行</HuiButton>
<HuiButton icon="minus" :disabled="count === 0" @click="count--">減一行</HuiButton>
<span>共 {{ count }} 行,超過 200px 後出現捲軸</span>
</HuiSpace>
<HuiScrollbar :max-height="200" class="box">
<p v-for="i in count" :key="i" class="row">第 {{ i }} 行內容</p>
</HuiScrollbar>
</HuiSpace>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
撐滿父元素
height 設定為 '100%' 時撐滿父元素,父元素要有確定的高度。下面的面板高 240px,標題以下的部分都是捲動區域:
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 條通知`)
</script>
<template>
<div class="panel">
<div class="panel__header">通知</div>
<div class="panel__body">
<HuiScrollbar height="100%">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</HuiScrollbar>
</div>
</div>
</template>
<style scoped>
.panel {
display: flex;
flex-direction: column;
height: 240px;
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.panel__header {
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-light);
font-weight: 600;
}
.panel__body {
flex: 1;
min-height: 0;
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
}
</style>
一直顯示
always 讓捲軸一直顯示,不再淡出。內容放得下的方向仍然不顯示。
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 20 }, (_, i) => `第 ${i + 1} 行`)
</script>
<template>
<HuiScrollbar always :height="200" class="box">
<div class="content">
<p v-for="item in items" :key="item" class="row">
{{ item }}:這一列寫得比較長,比區域寬,水平也能捲動
</p>
</div>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.content {
width: 720px;
padding: var(--hui-spacing-3) var(--hui-spacing-3) var(--hui-spacing-4);
}
.row {
margin: 0 0 var(--hui-spacing-2);
}
</style>
滑塊最小長度
內容很長時,滑塊按比例算出來會很短、不好拖。min-size 是滑塊的最小長度(px),預設 20。下面兩塊各有 1000 行:
<script setup lang="ts">
import { HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
const items = Array.from({ length: 1000 }, (_, i) => `第 ${i + 1} 行內容`)
</script>
<template>
<HuiSpace fill align="start">
<div>
<p class="label">預設(20px)</p>
<HuiScrollbar always :height="200" class="box">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</HuiScrollbar>
</div>
<div>
<p class="label">min-size="60"</p>
<HuiScrollbar always :height="200" :min-size="60" class="box">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</HuiScrollbar>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin: 0 0 var(--hui-spacing-2);
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-1) var(--hui-spacing-3);
}
</style>
捲動到指定位置
通過 ref 呼叫方法:
scrollTo(options):傳{ top, left, behavior },behavior: 'smooth'時平滑捲動;只傳top或left時另一個方向不動。scrollTo(x, y):直接滾到水平x、垂直y。setScrollTop(value)、setScrollLeft(value):只改一個方向。wrapRef是捲動區域元素,可以讀scrollHeight等尺寸。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
import type { ScrollbarExpose } from '@nscode/handy-ui'
const scrollbarRef = ref<ScrollbarExpose>()
const items = Array.from({ length: 40 }, (_, i) => `第 ${i + 1} 行`)
const toTopLeft = () => scrollbarRef.value?.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
const toBottom = () => {
const wrap = scrollbarRef.value?.wrapRef
if (wrap) scrollbarRef.value?.scrollTo({ top: wrap.scrollHeight, behavior: 'smooth' })
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton icon="top" @click="toTopLeft">平滑回到左上角</HuiButton>
<HuiButton icon="arrow-down" @click="toBottom">平滑滾到底端</HuiButton>
<HuiButton @click="scrollbarRef?.scrollTo(200, 300)">scrollTo(200, 300)</HuiButton>
<HuiButton @click="scrollbarRef?.setScrollTop(400)">setScrollTop(400)</HuiButton>
<HuiButton @click="scrollbarRef?.setScrollLeft(150)">setScrollLeft(150)</HuiButton>
</HuiSpace>
<HuiScrollbar ref="scrollbarRef" :height="220" class="box">
<div class="content">
<p v-for="item in items" :key="item" class="row">
{{ item }}:這一列比區域寬,水平也能捲動,方便看水平的方法
</p>
</div>
</HuiScrollbar>
</HuiSpace>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.content {
width: 900px;
padding: var(--hui-spacing-3) var(--hui-spacing-3) var(--hui-spacing-4);
}
.row {
margin: 0 0 var(--hui-spacing-2);
}
</style>
監聽捲動
scroll 事件的參數是 { scrollTop, scrollLeft }。滾輪、拖曳滑塊、點軌道、呼叫方法引起的捲動都會觸發。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
import type { ScrollbarScrollEvent } from '@nscode/handy-ui'
const scrollTop = ref(0)
const scrollLeft = ref(0)
const items = Array.from({ length: 40 }, (_, i) => `第 ${i + 1} 行`)
const handleScroll = (event: ScrollbarScrollEvent) => {
scrollTop.value = Math.round(event.scrollTop)
scrollLeft.value = Math.round(event.scrollLeft)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>scrollTop:{{ scrollTop }},scrollLeft:{{ scrollLeft }}</span>
<HuiScrollbar :height="200" class="box" @scroll="handleScroll">
<div class="content">
<p v-for="item in items" :key="item" class="row">{{ item }}:往下、往右捲動試試</p>
</div>
</HuiScrollbar>
</HuiSpace>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.content {
width: 800px;
padding: var(--hui-spacing-3) var(--hui-spacing-3) var(--hui-spacing-4);
}
.row {
margin: 0 0 var(--hui-spacing-2);
}
</style>
把 scroll 事件、setScrollTop 和 wrapRef 組合起來,可以讓滑塊和捲動位置互相聯動:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiScrollbar, HuiSlider, HuiSpace } from '@nscode/handy-ui'
import type { ScrollbarExpose, ScrollbarScrollEvent } from '@nscode/handy-ui'
const scrollbarRef = ref<ScrollbarExpose>()
const percent = ref(0)
const items = Array.from({ length: 50 }, (_, i) => `第 ${i + 1} 行內容`)
const maxScrollTop = () => {
const wrap = scrollbarRef.value?.wrapRef
return wrap ? wrap.scrollHeight - wrap.clientHeight : 0
}
// 拖曳滑塊時捲動內容
const handleSlider = (value: number | number[]) => {
percent.value = Array.isArray(value) ? value[0] : value
scrollbarRef.value?.setScrollTop((maxScrollTop() * percent.value) / 100)
}
// 捲動內容時同步滑塊
const handleScroll = ({ scrollTop }: ScrollbarScrollEvent) => {
const max = maxScrollTop()
percent.value = max > 0 ? Math.round((scrollTop / max) * 100) : 0
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSlider :model-value="percent" @update:model-value="handleSlider" />
<HuiScrollbar ref="scrollbarRef" :height="200" class="box" @scroll="handleScroll">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</HuiScrollbar>
</HuiSpace>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
原生捲軸
native 使用瀏覽器原生捲軸,只統一其色彩與粗細,不顯示自繪捲軸。原生捲軸持續顯示且佔用內容空間。下例由左至右為預設自繪捲軸、native 及未使用 Scrollbar 的瀏覽器捲軸。
<script setup lang="ts">
import { HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 行內容`)
</script>
<template>
<HuiSpace fill align="start">
<div>
<p class="label">自繪(預設)</p>
<HuiScrollbar :height="200" class="box">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</HuiScrollbar>
</div>
<div>
<p class="label">native</p>
<HuiScrollbar native :height="200" class="box">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</HuiScrollbar>
</div>
<div>
<p class="label">瀏覽器預設</p>
<div class="box plain">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</div>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin: 0 0 var(--hui-spacing-2);
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.plain {
height: 200px;
overflow: auto;
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
內容容器
元件裡有兩層:捲動區域(hui-scrollbar__wrap)和內部的內容容器(hui-scrollbar__view)。wrap-class、wrap-style 加在捲動區域上,view-class、view-style 加在內容容器上,tag 改內容容器的標籤。下面把內容容器渲染成 <ul>,清單項直接設定於預設插槽裡:
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const fruits = ['蘋果', '香蕉', '橙子', '葡萄', '西瓜', '草莓', '芒果', '菠蘿', '櫻桃', '桃子']
const wrapStyle = { background: 'var(--hui-fill-color-light)' }
const viewStyle = { margin: '0', padding: '8px 12px 8px 32px' }
</script>
<template>
<HuiScrollbar :height="160" tag="ul" :wrap-style="wrapStyle" :view-style="viewStyle" class="box">
<li v-for="fruit in fruits" :key="fruit" class="item">{{ fruit }}</li>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.item {
padding: var(--hui-spacing-1) 0;
}
</style>
鍵盤捲動
捲軸本身帶有 aria-hidden="true" 且不可聚焦,避免干擾螢幕閱讀器。若需鍵盤捲動,請為捲動區域設定 tabindex="0";聚焦後可使用方向鍵、PageUp、PageDown、Home、End 與空白鍵,並顯示捲軸及焦點外框。aria-label 提供區域名稱,傳入後區域帶有 role="region"。
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 條公告`)
</script>
<template>
<HuiScrollbar :height="200" tabindex="0" aria-label="公告清單" class="box">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
自訂外觀
捲軸的粗細、離邊緣的距離和顏色由下面幾個 CSS 變數決定,設定於元件的 style 或外層的類上就能覆蓋。native 時原生捲軸也用這幾個變數。公開變數不在元件根上重定義,下面刻意設定於祖先容器上;內外巢狀捲軸會繼承同一設定。懸停變數統一更名為 --hui-scrollbar-thumb-color-hover,不保留舊名後備值。
| 變數 | 預設值 | 說明 |
|---|---|---|
--hui-scrollbar-size | 6px | 捲軸粗細 |
--hui-scrollbar-gap | 2px | 捲軸離區域邊緣的距離 |
--hui-scrollbar-thumb-color | var(--hui-text-color-quaternary) | 滑塊顏色 |
--hui-scrollbar-thumb-color-hover | var(--hui-text-color-tertiary) | 懸停、拖曳時滑塊的顏色 |
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 行內容`)
</script>
<template>
<div class="primary">
<HuiScrollbar always :height="200" class="box">
<p v-for="item in items" :key="item" class="row">{{ item }}</p>
</HuiScrollbar>
</div>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.primary {
--hui-scrollbar-size: 10px;
--hui-scrollbar-thumb-color: var(--hui-color-primary-light-5);
--hui-scrollbar-thumb-color-hover: var(--hui-color-primary);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
Props
| 屬性 | 型別 | 預設值 | 說明 |
|---|---|---|---|
| height | number | string | — | 高度,數值以 px,字串原樣使用(如 '50vh'、'100%')。寫百分比時父元素要有確定的高度 |
| maxHeight | number | string | — | 最大高度,數值以 px。內容不夠高時跟隨內容走,超過時出現捲軸 |
| always | boolean | false | 一直顯示捲軸。預設只在懸停、捲動、拖曳和鍵盤聚焦時顯示 |
| minSize | number | 20 | 滑塊的最小長度(px)。不是數字或小於 0 時按 20 處理 |
| native | boolean | false | 用瀏覽器原生捲軸,只統一顏色和粗細,不顯示自繪的捲軸 |
| wrapClass | ClassValue | — | 捲動區域(hui-scrollbar__wrap)的類名 |
| wrapStyle | StyleValue | — | 捲動區域的樣式 |
| viewClass | ClassValue | — | 內容容器(hui-scrollbar__view)的類名 |
| viewStyle | StyleValue | — | 內容容器的樣式 |
| tag | string | 'div' | 內容容器的標籤 |
| tabindex | number | string | — | 捲動區域的 tabindex。設成 0 後能用 Tab 聚焦,再用方向鍵等按鍵捲動 |
| ariaLabel | string | — | 捲動區域的無障礙名稱。傳入以後捲動區域帶 role="region" |
Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| scroll | (event: ScrollbarScrollEvent) | 捲動時觸發,參數是 { scrollTop, scrollLeft }。拖曳、點軌道、呼叫方法引起的捲動也會觸發 |
Slots
| 插槽 | 說明 |
|---|---|
| default | 要捲動的內容,放在內容容器裡 |
方法
| 方法 | 參數 | 說明 |
|---|---|---|
| scrollTo | (options: ScrollToOptions) 或 (x, y) | 捲動到指定位置。選項物件可以帶 behavior: 'smooth',只傳一個方向時另一個方向不動 |
| setScrollTop | (value: number) | 設定垂直捲動位置(px) |
| setScrollLeft | (value: number) | 設定水平捲動位置(px) |
| update | — | 重新計算滑塊的大小和位置。尺寸變化一般會自動更新,見注意事項 |
| wrapRef | — | 不是方法,是捲動區域元素(HTMLElement),可以讀 scrollHeight 等尺寸 |
型別
import type {
ScrollbarProps,
ScrollbarEmits,
ScrollbarScrollEvent,
ScrollbarScrollToOptions,
ScrollbarExpose
} from '@nscode/handy-ui'
interface ScrollbarScrollEvent {
scrollTop: number
scrollLeft: number
}
// 同 DOM 的 ScrollToOptions:{ left?: number; top?: number; behavior?: 'auto' | 'smooth' | 'instant' }
type ScrollbarScrollToOptions = ScrollToOptions
interface ScrollbarExpose {
wrapRef: HTMLElement | undefined
scrollTo: {
(options: ScrollbarScrollToOptions): void
(x: number, y: number): void
}
setScrollTop: (value: number) => void
setScrollLeft: (value: number) => void
update: () => void
}