Backtop
Show a button in the bottom-right corner after scrolling a specified distance. Click it to return to the top.
Import
import { HuiBacktop } from '@nscode/handy-ui'
Basic Usage
Without target, the component monitors page scrolling. The button appears after visibility-height (200px by default) and scrolls smoothly to the top when clicked. It uses position: fixed, 40px from the viewport's right and bottom edges by default.
Examples on this page use fixed-height scrollable containers referenced by target; see Custom Scroll Containers below. Their outer wrappers use transform to position the button within the example, which is not needed in normal use. Scroll down in the box:
<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">Row {{ i }} content</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>
Custom Scroll Containers
For content in a scrollable element, pass a CSS selector or the element to target. It must be the element that actually scrolls, with a fixed height and overflow: auto or scroll, rather than an outer wrapper. A selector that matches nothing falls back to page scrolling.
<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">Scroll down within this area</div>
<HuiBacktop target=".backtop-demo" :visibility-height="100" />
</div>
</template>
The button always uses position: fixed at the viewport's bottom-right; it does not follow the container. The example above therefore places it in the window's bottom-right. Adjust right and bottom to move it beside a differently positioned container.
Position and Visibility Threshold
right and bottom are offsets in pixels from the window's right and bottom edges. visibility-height is the scroll distance at which the button appears. The buttons below have thresholds of 100, 300, and 600px and appear in that order as you scroll:
<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">Row {{ i }} content</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>
Scroll Behavior
behavior accepts smooth (the default) or auto (jump directly to the top). Scroll down and click each button to compare:
<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">Row {{ i }} content</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">Row {{ i }} content</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>
Icons and Text
icon changes the button's icon using an HuiIcon name, defaulting to arrow-up. text adds a label to the right of the icon, giving the button a pill shape.
<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">Row {{ i }} content</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="Back to top"
: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>
Custom Content
The default slot replaces all button content, including the icon and text. The root element is already a <button>; do not nest another button in the slot.
<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">Row {{ i }} content</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>
Reading Visibility with v-model:visible
Without visible, the component determines visibility from the scroll position (uncontrolled mode). Crossing the threshold emits update:visible, allowing v-model:visible to track visibility. visibleChange is emitted when the button actually appears or disappears. Clicking emits click before scrolling starts.
<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('Back to top clicked')
}
</script>
<template>
<div class="demo">
<div id="backtop-demo-model" class="box">
<p v-for="i in 30" :key="i" class="line">Row {{ i }} content</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">
Button: {{ visible ? 'Visible' : 'Hidden' }}; visibleChange emitted {{ changeCount }} times
</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>
Controlled Visibility
Providing :visible without handling update:visible is controlled mode: the prop fully determines visibility, and scrolling does not change it. For example, allow visibility only under certain conditions:
<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="Allow Back to top" />
<span>Threshold crossed:{{ passed ? 'Yes' : 'No' }}</span>
</HuiSpace>
<div class="demo">
<div id="backtop-demo-controlled" class="box">
<p v-for="i in 30" :key="i" class="line">Row {{ i }} content</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>
In the example above, update:visible reports whether the threshold has been crossed, while enabled && passed determines actual visibility.
Keyboard Interaction
The native <button> receives Tab focus, and Enter or Space activates it. Its accessible name is text, or the localized default when omitted, such as Back to top in English.
Prop Presence and Automatic Visibility
When visible is omitted or only an update listener is provided, automatic visibility is maintained internally from false. Prop presence enables controlled mode, including undefined (displayed as false). Threshold crossings emit requests; if the parent rejects one, the button remains hidden and no visibleChange is emitted. Removing the prop restores the previous internal state, which then follows threshold crossings again. Mode changes warn in development. Scroll targets, throttling, and event order retain their existing behavior.
<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('No requests yet')
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)">Cross the threshold</HuiButton
><HuiButton class="q1-low" @click="q1Scroll(0)">Return below the threshold</HuiButton
><HuiButton class="q1-redraw" @click="q1Redraw++"
>Parent redraw {{ q1Redraw }}</HuiButton
></HuiSpace
>
<p>
Above 100px: local, listener-only, and two-way-bound buttons appear; explicit undefined and
parent rejection remain hidden.
</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">Scroll area</div>
</div>
<p class="q1-notification">Bound value:{{ q1Model }}; latest request:{{ q1Last }}</p>
</HuiSpace>
<div class="q1-local">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="20"
:bottom="48"
behavior="auto"
text="Local"
/>
</div>
<div class="q1-listener">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="112"
:bottom="48"
behavior="auto"
text="Listener"
@update:visible="q1Last = String($event)"
/>
</div>
<div class="q1-empty">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="204"
:bottom="48"
behavior="auto"
text="Undefined"
:visible="undefined"
/>
</div>
<div class="q1-fixed">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="296"
:bottom="48"
behavior="auto"
text="Rejected"
:visible="false"
/>
</div>
<div class="q1-bound">
<HuiBacktop
:target="q1Target"
:visibility-height="100"
:right="388"
:bottom="48"
behavior="auto"
text="Bound"
v-model:visible="q1Model"
/>
</div>
</div>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| target | string | HTMLElement | Window | - | Scroll container, or a CSS selector for it. Omission or an unmatched selector falls back to page scrolling (window) |
| visibilityHeight | number | 200 | Scroll distance in pixels at or above which the button appears |
| right | number | 40 | Distance from the viewport's right edge in pixels |
| bottom | number | 40 | Distance from the viewport's bottom edge in pixels |
| behavior | 'smooth' | 'auto' | 'smooth' | Scroll behavior when returning to the top |
| visible | boolean | - | Button visibility, supporting v-model:visible. Prop presence enables controlled mode (including undefined, displayed as false); omission enables automatic visibility based on scroll position |
| icon | string | 'arrow-up' | Button icon, using an HuiIcon name |
| text | string | - | Text to the right of the icon, also used as the accessible name |
Events
| Event | Callback arguments | Description |
|---|---|---|
| click | (event: MouseEvent) | Emitted before scrolling when clicked, or activated with Enter/Space while focused |
| update:visible | (value: boolean) | Emitted when scrolling crosses visibilityHeight in either direction, with the visibility calculated from the scroll position. Emitted in controlled and uncontrolled modes |
| visibleChange | (value: boolean) | Emitted when actual visibility changes: from scrolling in uncontrolled mode or changes to visible in controlled mode. Not emitted on mount |
Slots
| Slot | Scope arguments | Description |
|---|---|---|
| default | - | Button content, replacing the default icon and text |
Types
Public types use the component prefix. The compatibility mapping is ScrollBehavior → BacktopScrollBehavior. The old name remains importable from @nscode/handy-ui, is marked @deprecated, and will be removed in 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]
}