BackTop 回到顶部
在页面或指定滚动容器达到阈值后展示回顶入口。
引入
uni-app 配置 easycom 后使用 <hui-back-top>。公开类型和函数从 @nscode/handy-uni 导入;普通 Vue 3 安装 @nscode/handy-uni/web 预览插件。
示例
示例可独立运行;交互结果直接显示在页面。
vue
<template>
<view>
<hui-space direction="vertical" fill>
<hui-button custom-class="hui-space__item" @click="top = top === 0 ? 500 : 0"
>模拟滚动 / Simulate scroll</hui-button
>
<text class="hui-space__item">{{ top }} / {{ lastEvent }}</text>
<hui-back-top
custom-class="hui-space__item"
:scroll-top="top"
:target="target"
text="顶部 / Top"
@click="lastEvent = 'click'"
@visible-change="(value) => (lastEvent = String(value))"
/>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const top = ref(0)
const target = {
scrollTo: (value: number) => {
top.value = value
}
}
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
scrollTop | number | — | 当前滚动位置;uni 版必传(页面转发),Web 版不传时自己订阅窗口滚动 |
target | 'page' | BackTopTarget | 'page' | 回到哪里的顶部:页面,或者一个有 scrollTo(top, duration?) 方法的对象(内部滚动容器) |
visibilityHeight | number | 200 | 滚过多少 px 才出现 |
visible | boolean | undefined | 传了就按它显示,不看滚动距离;受控布尔,声明成 default: undefined |
right | number | 16 | 距右边,px |
bottom | number | 100 | 距底边,px,另加底部安全区 |
duration | number | 300 | 滚动动画时长,ms;0 立即到顶。PC 用 behavior,uni 的 pageScrollTo 只认时长,所以改名 |
icon | string | 'top' | PC 同名 |
text | string | — | 图标下的文字 |
shape | BackTopShape | 'circle' | shape 属性。 |
zIndex | number | 10 | 在页面内容之上、弹层之下 |
根节点类名和样式使用 customClass、customStyle。尺寸数字按 px,时长除明确说明外按 ms。
Events
| 事件 | 参数 |
|---|---|
click | (event: unknown) |
visible-change | (visible: boolean) |
受控状态由父级回写更新事件后改变。多词事件使用短横线。禁用操作、取消及过期异步结果不提交成功变更。
Slots
| 插槽 | 作用域 |
|---|---|
default | — |
方法
无。
类型
BackTopTarget、BackTopShape、BackTopProps、BackTopEmits.
平台说明
uni-app 页面必须在 onPageScroll 中把 e.scrollTop 转给 scroll-top;内部滚动目标传有 scrollTo(top,duration) 的对象。普通 Web 可自动订阅页面滚动;KeepAlive 失活会解绑。
设备冒烟清单.