Skip to content

Backtop 回到顶部

用于返回页面顶部的操作按钮。

组件类型

基本用法

由于回到顶部需要实时监听页面滚动位置,通常需要在页面的 onPageScroll 生命周期中获取滚动距离,再通过 scroll-top 传递给组件。

html
<wd-backtop :scroll-top="scrollTop"></wd-backtop>
ts
import { onPageScroll } from '@dcloudio/uni-app'
import { ref } from 'vue'

const scrollTop = ref(0)

onPageScroll((event) => {
  scrollTop.value = event.scrollTop
})

组件变体

形状与文字

通过 shape 切换圆形或方形按钮,通过 text 显示按钮文字。

html
<wd-backtop :scroll-top="scrollTop" shape="square" text="TOP"></wd-backtop>

自定义图标

默认插槽可以替换按钮内部内容。

html
<wd-backtop :scroll-top="scrollTop">
  <text>TOP</text>
</wd-backtop>

组件样式

自定义显示距离

通过 top 设置滚动距离超过多少时显示按钮。

html
<wd-backtop :scroll-top="scrollTop" :top="600"></wd-backtop>

自定义样式

通过 custom-styleicon-style 调整按钮与图标样式。

html
<wd-backtop :scroll-top="scrollTop" custom-style="background: #007aff;color:white;" icon-style="color: white;"></wd-backtop>

自定义滚动时间

通过 duration 设置返回顶部滚动时长,单位为毫秒。

html
<wd-backtop :scroll-top="scrollTop" :duration="1000"></wd-backtop>

Backtop Attributes

参数说明类型默认值
scroll-top页面滚动距离number-
text按钮文字string-
top滚动距离超过该值时显示按钮,单位为 pxnumber300
duration返回顶部滚动时间,单位为 msnumber100
z-index组件层级number10
icon-style自定义图标样式string''
shape按钮形状,可选值为 circlesquarestringcircle
bottom距离屏幕底部的距离,单位为 pxnumber100
right距离屏幕右侧的距离,单位为 pxnumber20
custom-style自定义根节点样式string''
custom-class自定义根节点样式类string''

Backtop Slots

名称说明
default自定义按钮内容

主题定制

CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 全局配置

名称默认值描述
--wot-backtop-bg$filled-oppo背景色
--wot-backtop-border-color$border-main边框颜色
--wot-backtop-width$n-48按钮宽度
--wot-backtop-height$n-48按钮高度
--wot-backtop-radius$radius-main方形模式圆角
--wot-backtop-radius-circle$radius-radius-full圆形模式圆角
--wot-backtop-color$icon-main按钮图标颜色
--wot-backtop-icon-size$n-20按钮图标尺寸
--wot-backtop-text-font-size$typography-label-size-main文本字号
--wot-backtop-text-color$text-main文本颜色
--wot-backtop-text-line-height$typography-label-line--height-size-main文本行高
--wot-backtop-z-index100组件层级

源代码

文档
组件

Released under the MIT License.

Released under the MIT License.