Skip to content

BackTop 回到顶部

返回页面顶部的操作按钮。滚动超过一定距离后出现,点击平滑回到顶部。底层是原生 <button>,开箱即用支持键盘与屏幕阅读器。

基础用法

默认监听整个页面,滚动超过 visibility-height(默认 200px)后在右下角出现。下面在一个独立滚动容器内演示,按钮固定在视口角落。

↓ 向下滚动这个区域,超过 120px 后视口右下角会出现「回到顶部」按钮
vue
<template>
  <!-- 监听整个页面:滚动超过 200px 后显示 -->
  <ExBackTop />

  <!-- 监听指定滚动容器 -->
  <ExBackTop :target="() => containerEl" :visibility-height="120" />
</template>

显示滚动进度

show-progress 显示滚动进度,progress-type 支持环形(circle)与线形(line),show-percent 额外显示百分比文字。

↓ 滚动此区域,左下角出现带环形进度 + 百分比的按钮
vue
<template>
  <ExBackTop show-progress progress-type="circle" show-percent />
  <ExBackTop show-progress progress-type="line" />
</template>

文字按钮

设置 text 后按钮变为图标 + 文字的纵向布局。

↓ 滚动此区域,右上角出现带「顶部」文字的按钮
vue
<template>
  <ExBackTop text="顶部" />
</template>

形状与尺寸

shape 支持 circle / square / roundedsize 支持 small / medium / large

vue
<template>
  <ExBackTop shape="circle" size="small" />
  <ExBackTop shape="rounded" size="medium" />
  <ExBackTop shape="square" size="large" />
</template>

自定义图标 / 内容

通过 icon 传入内置图标名(推荐)、组件或 VNode;也可以用默认插槽完全自定义内容。

↓ 滚动此区域,右下角出现使用自定义图标的按钮
vue
<template>
  <!-- 内置图标名 -->
  <ExBackTop icon="arrow-up-s-line" />

  <!-- 完全自定义内容 -->
  <ExBackTop #default="{ percent }">
    <span>{{ Math.round(percent) }}%</span>
  </ExBackTop>
</template>

API

Props

属性说明类型默认值
position显示位置'bottom-right' | 'bottom-left' | 'top-right' | 'top-left' | 'bottom-center' | 'top-center''bottom-right'
shape形状'circle' | 'square' | 'rounded''circle'
size尺寸'small' | 'medium' | 'large''medium'
visibility-height滚动超过该距离(px)后显示number200
visibility-percent滚动超过该百分比(0-100)后显示,设置后优先于 visibility-heightnumber-
target滚动监听目标,默认监听 windowHTMLElement | Window | string | (() => HTMLElement | Window | string)window
scroll-behavior点击后的滚动行为'smooth' | 'auto' | 'instant''smooth'
scroll-duration平滑滚动时长(ms),scroll-behavior='smooth' 时生效number400
scroll-to点击后滚动到的位置(px)number0
offset位置偏移(默认四周 24px){ top?, right?, bottom?, left?: number | string }-
animation出现/消失动画'fade' | 'slide' | 'zoom' | 'none''fade'
text按钮文字string-
icon自定义图标(内置图标名 / 组件 / VNode / 字体类名)string | Component | VNode'arrow-up-line'
show-progress是否显示滚动进度booleanfalse
progress-type进度类型'circle' | 'line''circle'
show-percent是否显示百分比文字booleanfalse
z-index层级numbervar(--ex-z-sticky)
show-on-mobile是否在移动端显示booleantrue
mobile-breakpoint移动端断点(px)number768
aria-label无障碍标签stringtext'Back to top'
class / style自定义类名 / 样式string | string[] | object / CSSProperties | string-

Events

事件名说明回调参数
click点击按钮时触发(event: MouseEvent)
scroll-start回到顶部滚动开始-
scroll-end回到顶部滚动结束-
visibility-change显示状态变化(visible: boolean)

Slots

插槽名说明参数
default完全自定义按钮内容{ percent: number }
icon自定义图标-

方法(通过 ref 调用)

方法名说明类型
scrollToTop滚动到目标位置() => Promise<void>
show / hide强制显示 / 隐藏() => void
getElement获取按钮 DOM() => HTMLElement | null

无障碍

  • 使用原生 <button>:默认可聚焦,Enter / Space 即可触发,无需额外配置。
  • 提供 aria-label(默认取 text'Back to top')。
  • 进度图形标记 aria-hidden,不干扰屏幕阅读器。
  • 遵循 prefers-reduced-motion:用户偏好减少动画时关闭过渡。

从旧版本迁移

为提升质量与可维护性,本组件做了精简。已移除以下非标准能力:draggable / drag-bounds / remember-position / storage-key(拖拽与位置记忆)、内置性能监控、theme 主题对象、enabledshow-textthrottle-delaybounce 动画,以及 onClick/onScrollStart 等 prop 形式回调(请改用事件)。

迁移要点:

  • show-text + text → 直接用 text(设置即显示)。
  • :enabled="false" → 用 v-if 控制是否渲染。
  • @click 等请用事件监听(onClick prop 已移除)。
  • 主题定制改用全局 CSS 令牌(见下)。

主题

组件跟随全局主题令牌,可覆盖以下变量微调:

css
:root {
  --ex-color-bg-secondary: #0f1720;   /* 按钮背景 */
  --ex-color-border-primary: #243447;  /* 按钮边框 */
  --ex-color-primary: #29abe2;         /* 悬停 / 进度色 */
}