Backtop 回到頂部
頁面捲動到一定距離後,在右下角顯示一個按鈕,點了滾回頂部。
引入
import { HuiBacktop } from '@nscode/handy-ui'
基礎用法
不傳 target 時監聽頁面捲動。滾過 visibility-height(預設 200px)後按鈕出現,點擊平滑滾回頂部。按鈕用 position: fixed 定位,預設在瀏覽器視窗右下角,離右邊、下邊各 40px。
為了方便看效果,本頁的示例都放在固定高度的捲動容器裡,用 target 指向這個容器(用法見下面的“自訂捲動容器”);外層示例框加了 transform,讓按鈕顯示在示例框的右下角,實際使用時不用寫。在框裡往下滾:
<script setup lang="ts">
import { HuiBacktop } from '@nscode/handy-ui'
</script>
<template>
<div class="demo">
<div id="backtop-demo-basic" class="box">
<p v-for="i in 30" :key="i" class="line">第 {{ i }} 行內容</p>
</div>
<HuiBacktop target="#backtop-demo-basic" :right="24" :bottom="16" />
</div>
</template>
<style scoped>
/* transform 让按钮相对示例框定位,实际使用时不用写 */
.demo {
transform: translateZ(0);
}
.box {
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
自訂捲動容器
內容在某個可以捲動的元素裡時,用 target 指定這個元素,可以傳 CSS 選擇器或元素。target 必須是真正捲動的那個元素(設了固定高度和 overflow: auto 或 scroll),不能是它外面的包裹元素;選擇器找不到時退回監聽頁面捲動。
<script setup lang="ts">
import { HuiBacktop } from '@nscode/handy-ui'
</script>
<template>
<div
class="backtop-demo"
style="height: 300px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<div style="height: 1000px; padding: 16px">在這個區域裡往下捲動</div>
<HuiBacktop target=".backtop-demo" :visibility-height="100" />
</div>
</template>
按鈕始終用 position: fixed 定位在瀏覽器視窗的右下角,不會跟著容器走:上面這個示例的按鈕出現在視窗右下角。容器不在右下角時,用 right、bottom 把按鈕挪到容器旁邊。
位置和顯示閾值
right、bottom 是按鈕離視窗右邊和下邊的距離(px),visibility-height 是滾過多少像素後顯示按鈕。下面三個按鈕的閾值分別是 100、300、600px,往下滾時依次出現:
<script setup lang="ts">
import { HuiBacktop } from '@nscode/handy-ui'
</script>
<template>
<div class="demo">
<div id="backtop-demo-threshold" class="box">
<p v-for="i in 40" :key="i" class="line">第 {{ i }} 行內容</p>
</div>
<HuiBacktop
target="#backtop-demo-threshold"
:visibility-height="100"
:right="24"
:bottom="16"
text="100px"
/>
<HuiBacktop
target="#backtop-demo-threshold"
:visibility-height="300"
:right="24"
:bottom="72"
text="300px"
/>
<HuiBacktop
target="#backtop-demo-threshold"
:visibility-height="600"
:right="24"
:bottom="128"
text="600px"
/>
</div>
</template>
<style scoped>
/* transform 让按钮相对示例框定位,实际使用时不用写 */
.demo {
transform: translateZ(0);
}
.box {
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
捲動方式
behavior 可選 smooth(平滑捲動,預設)和 auto(直接跳到頂部)。分別滾下去再點按鈕對比:
<script setup lang="ts">
import { HuiBacktop, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap align="start">
<div class="demo">
<div id="backtop-demo-smooth" class="box">
<p v-for="i in 30" :key="i" class="line">第 {{ i }} 行內容</p>
</div>
<HuiBacktop
target="#backtop-demo-smooth"
behavior="smooth"
text="smooth"
:visibility-height="100"
:right="24"
:bottom="16"
/>
</div>
<div class="demo">
<div id="backtop-demo-auto" class="box">
<p v-for="i in 30" :key="i" class="line">第 {{ i }} 行內容</p>
</div>
<HuiBacktop
target="#backtop-demo-auto"
behavior="auto"
text="auto"
:visibility-height="100"
:right="24"
:bottom="16"
/>
</div>
</HuiSpace>
</template>
<style scoped>
/* transform 让按钮相对示例框定位,实际使用时不用写 */
.demo {
width: 260px;
transform: translateZ(0);
}
.box {
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
圖示和文字
icon 換按鈕的圖示(HuiIcon 的 name),預設 arrow-up;text 在圖示右邊加上文字,按鈕變成兩頭半圓的膠囊。
<script setup lang="ts">
import { HuiBacktop } from '@nscode/handy-ui'
</script>
<template>
<div class="demo">
<div id="backtop-demo-icon" class="box">
<p v-for="i in 30" :key="i" class="line">第 {{ i }} 行內容</p>
</div>
<HuiBacktop target="#backtop-demo-icon" :visibility-height="100" :right="24" :bottom="16" />
<HuiBacktop
target="#backtop-demo-icon"
icon="top"
:visibility-height="100"
:right="80"
:bottom="16"
/>
<HuiBacktop
target="#backtop-demo-icon"
icon="chevron-up"
:visibility-height="100"
:right="136"
:bottom="16"
/>
<HuiBacktop
target="#backtop-demo-icon"
icon="top"
text="回到頂部"
:visibility-height="100"
:right="24"
:bottom="72"
/>
</div>
</template>
<style scoped>
/* transform 让按钮相对示例框定位,实际使用时不用写 */
.demo {
transform: translateZ(0);
}
.box {
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
自訂內容
預設插槽替換按鈕裡的全部內容(圖示和文字)。元件的根元素本身就是 <button>,插槽裡不要再放按鈕。
<script setup lang="ts">
import { HuiBacktop } from '@nscode/handy-ui'
</script>
<template>
<div class="demo">
<div id="backtop-demo-slot" class="box">
<p v-for="i in 30" :key="i" class="line">第 {{ i }} 行內容</p>
</div>
<HuiBacktop target="#backtop-demo-slot" :visibility-height="100" :right="24" :bottom="16">
<span style="color: var(--hui-color-primary); font-weight: bold">UP</span>
</HuiBacktop>
</div>
</template>
<style scoped>
/* transform 让按钮相对示例框定位,实际使用时不用写 */
.demo {
transform: translateZ(0);
}
.box {
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
用 v-model:visible 獲取顯示狀態
沒傳 visible 時,按鈕是否顯示由元件按捲動位置決定(非受控)。捲動越過閾值時,元件會觸發 update:visible,所以可以用 v-model:visible 拿到現在是否顯示。按鈕實際顯示或隱藏時還會觸發 visibleChange;點擊按鈕時先觸發 click,再開始捲動。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBacktop, HuiMessage } from '@nscode/handy-ui'
const visible = ref(false)
const changeCount = ref(0)
const handleVisibleChange = () => {
changeCount.value += 1
}
const handleClick = () => {
HuiMessage.info('點了回到頂部')
}
</script>
<template>
<div class="demo">
<div id="backtop-demo-model" class="box">
<p v-for="i in 30" :key="i" class="line">第 {{ i }} 行內容</p>
</div>
<HuiBacktop
v-model:visible="visible"
target="#backtop-demo-model"
:visibility-height="100"
:right="24"
:bottom="16"
@visible-change="handleVisibleChange"
@click="handleClick"
/>
</div>
<p style="margin-top: 12px">
按鈕{{ visible ? '顯示中' : '已隱藏' }},visibleChange 觸發了 {{ changeCount }} 次
</p>
</template>
<style scoped>
/* transform 让按钮相对示例框定位,实际使用时不用写 */
.demo {
transform: translateZ(0);
}
.box {
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
受控顯示
只傳 :visible(不處理 update:visible)時是受控模式,按鈕顯示不顯示完全由 visible 決定,捲動不會改變它。比如只在某些條件下才允許顯示:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBacktop, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const enabled = ref(false)
const passed = ref(false)
const handleUpdate = (value: boolean) => {
passed.value = value
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiSwitch v-model="enabled" active-text="允許顯示回到頂部" />
<span>滾過閾值:{{ passed ? '是' : '否' }}</span>
</HuiSpace>
<div class="demo">
<div id="backtop-demo-controlled" class="box">
<p v-for="i in 30" :key="i" class="line">第 {{ i }} 行內容</p>
</div>
<HuiBacktop
target="#backtop-demo-controlled"
:visibility-height="100"
:right="24"
:bottom="16"
:visible="enabled && passed"
@update:visible="handleUpdate"
/>
</div>
</HuiSpace>
</template>
<style scoped>
/* transform 让按钮相对示例框定位,实际使用时不用写 */
.demo {
transform: translateZ(0);
}
.box {
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
上面的例子裡 update:visible 告訴外面有沒有滾過閾值,是否顯示由 enabled && passed 決定。
鍵盤操作
按鈕是原生的 <button>,可以用 Tab 鍵聚焦,按 Enter 或空白等同於點擊。螢幕閱讀器軟體讀出的名稱是 text,沒傳 text 時隨語言(中文是“回到頂部”)。
屬性存在性與自動顯示
未傳visible或僅監聽更新時從false保存自動顯示狀態。傳入屬性即受控,包括undefined(按false顯示);捲動跨閾值只發請求,父拒絕不顯示,也不發visibleChange。移除屬性恢復原內部狀態,之後按捲動跨閾值繼續更新;模式切換在開發環境警告。捲動目標、節流及事件先後保持原約定。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBacktop, HuiButton, HuiSpace } from '@nscode/handy-ui'
const q1Target = ref<HTMLDivElement>()
const q1Redraw = ref(0)
const q1Last = ref('尚無請求')
const q1Model = ref(false)
const q1Scroll = (top: number) => {
const target = q1Target.value
if (target) {
target.scrollTop = top
target.dispatchEvent(new Event('scroll'))
}
}
</script>
<template>
<div class="backtop-q1-demo" style="position: relative; transform: translateZ(0)">
<HuiSpace direction="vertical" fill>
<HuiSpace
><HuiButton class="q1-high" @click="q1Scroll(300)">滾過閾值</HuiButton
><HuiButton class="q1-low" @click="q1Scroll(0)">回到閾值內</HuiButton
><HuiButton class="q1-redraw" @click="q1Redraw++"
>父重新呈現 {{ q1Redraw }}</HuiButton
></HuiSpace
>
<p>超過100px:自持、僅監聽和雙向綁定顯示;顯式undefined、父拒絕保持隱藏。</p>
<div
ref="q1Target"
class="q1-scroll-target"
style="
height: 160px;
overflow: auto;
border: 1px solid var(--hui-border-color);
"
>
<div style="height: 800px; padding: 16px">捲動區域</div>
</div>
<p class="q1-notification">綁定值:{{ q1Model }};最近請求:{{ q1Last }}</p>
</HuiSpace>
<div class="q1-local">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="20"
:bottom="48"
behavior="auto"
text="自持"
/>
</div>
<div class="q1-listener">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="112"
:bottom="48"
behavior="auto"
text="監聽"
@update:visible="q1Last = String($event)"
/>
</div>
<div class="q1-empty">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="204"
:bottom="48"
behavior="auto"
text="空值"
:visible="undefined"
/>
</div>
<div class="q1-fixed">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="296"
:bottom="48"
behavior="auto"
text="拒絕"
:visible="false"
/>
</div>
<div class="q1-bound">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="388"
:bottom="48"
behavior="auto"
text="綁定"
v-model:visible="q1Model"
/>
</div>
</div>
</template>
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| target | string | HTMLElement | Window | - | 監聽捲動的容器,字串按 CSS 選擇器查找。不傳、或選擇器找不到時監聽頁面捲動(window) |
| visibilityHeight | number | 200 | 捲動距離大於等於這個值(px)時顯示按鈕 |
| right | number | 40 | 按鈕離瀏覽器視窗右邊的距離(px) |
| bottom | number | 40 | 按鈕離瀏覽器視窗下邊的距離(px) |
| behavior | 'smooth' | 'auto' | 'smooth' | 回到頂部時的捲動方式 |
| visible | boolean | - | 是否顯示按鈕,支援 v-model:visible。傳入即受控(含undefined,按false顯示),顯示完全由它決定;不傳時按捲動位置自動顯示 |
| icon | string | 'arrow-up' | 按鈕圖示,HuiIcon 的 name |
| text | string | - | 圖示右邊的文字,同時用作按鈕的可讀名稱 |
Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| click | (event: MouseEvent) | 點擊按鈕(或聚焦後按 Enter、空白)時觸發,在開始捲動之前 |
| update:visible | (value: boolean) | 捲動越過 visibilityHeight(從下到上或從上到下)時觸發,參數是按捲動位置算出來該不該顯示。受控和非受控時都會觸發 |
| visibleChange | (value: boolean) | 按鈕實際顯示或隱藏時觸發。非受控時隨捲動觸發,受控時隨 visible 變化觸發;掛載時不觸發 |
Slots
| 插槽 | 作用域參數 | 說明 |
|---|---|---|
| default | - | 按鈕裡的內容,替換預設的圖示和文字 |
類型
公開類型使用元件前綴。相容映射:ScrollBehavior → BacktopScrollBehavior。舊名仍可從 @nscode/handy-ui 導入,已標記 @deprecated,將在 1.0 刪除。
import type {
BacktopProps,
BacktopEmits,
BacktopTarget,
BacktopScrollBehavior
} from '@nscode/handy-ui'
type BacktopTarget = string | HTMLElement | Window
type BacktopScrollBehavior = 'smooth' | 'auto'
interface BacktopProps {
target?: BacktopTarget
visibilityHeight?: number
right?: number
bottom?: number
behavior?: BacktopScrollBehavior
visible?: boolean
icon?: string
text?: string
}
interface BacktopEmits {
click: [event: MouseEvent]
'update:visible': [value: boolean]
visibleChange: [value: boolean]
}