WebberUI

Custom Scrollbar

可客製的動畫自訂捲軸,spring 追隨捲動、滑塊可拖曳、點軌道跳轉。

motion/react spring 驅動的自訂捲軸:隱藏原生捲軸,改用可拖曳、可點軌道跳轉、支援漸層與 autoHide 的滑塊,並在捲動時帶一點慣性回饋。

載入預覽⋯

Playground

即時調整 props、程式碼片段同步更新——直接試出你要的樣子再複製。

300
8
<CustomScrollbar />

安裝

npx shadcn@latest add https://webberui.com/r/custom-scrollbar.json

或在 components.json 設定 registries 後,改用 @webberui/custom-scrollbar 安裝。

安裝依賴後,從 registry JSON(/r/custom-scrollbar.jsonfiles[0].content)複製 custom-scrollbar.tsx 原始碼到你的 components/ui/ 目錄:

npm install motion clsx tailwind-merge

使用

import { CustomScrollbar } from "@/components/ui/custom-scrollbar";

<CustomScrollbar maxHeight={280} width={9} autoHide>
  <div className="p-5">{/* 你的長內容 */}</div>
</CustomScrollbar>

maxHeight 決定內容區高度,超出即可捲動。滑塊高度會依內容比例自動計算。傳入 CSS 漸層字串到 thumbColor 即可做出彩色捲軸。

Props

Prop型別預設值說明
childrenReact.ReactNode要包裹並可捲動的內容
maxHeightnumber | string300內容區最大高度,超出即可捲動
widthnumber8捲軸寬度(px)
thumbColorstring靛紫漸層滑塊顏色,可傳 CSS 漸層字串
thumbHoverColorstring滑鼠移入捲軸時的滑塊顏色
trackColorstringrgba(120,120,140,0.14)軌道底色
showTrackbooleantrue是否顯示軌道底色
autoHidebooleanfalse靜止時淡出,捲動、hover 或拖曳時淡入

可及性

  • 使用者系統開啟「減少動態效果」時,滑塊即時追隨捲動、不套用 spring 慣性
  • 原生捲動行為完整保留(滾輪、觸控、鍵盤);自訂滑塊層帶 aria-hidden,僅作視覺回饋
  • 嚴禁使用 100vh 等視口高度單位;請以 maxHeight 給定明確界線

On this page