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 键聚焦,按回车或空格等同于点击。读屏软件读出的名称是 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) | 点击按钮(或聚焦后按回车、空格)时触发,在开始滚动之前 |
| 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]
}