Navigation · BackTop

BackTop

Demos

How to import

<script setup lang="ts">
    import { BackTop } from '@fibrous/semi-ui-vue';
</script>

Basic Usage

BackTop is a FloatButton that appears once target scrolls past visibilityHeight and scrolls it back to the top when clicked. It shows a back-to-top icon by default, and its accessible name comes from the locale by default ('Back to top' in English), which aria-label can override.

Scroll down to see the bottom-right gray button.
Code
<script setup lang="ts">
    import { BackTop } from '@fibrous/semi-ui-vue';
</script>

<template>
    <div>
        <span>Scroll down to see the bottom-right gray button.</span>
        <BackTop aria-label="Back to top" />
    </div>
</template>

Customized Style

BackTop sits 50px from the bottom and 100px from the right (the left in RTL) by default, which style can override.

Its look is customized with the #icon slot and the FloatButton props shape, size, colorful and badge.

向下滚动,右下角会出现一个方形的多彩按钮。
Code
<script setup lang="ts">
    import { IconArrowUp } from '@fibrous/semi-icons-vue';
    import { BackTop } from '@fibrous/semi-ui-vue';
</script>

<template>
    <div>
        <span>向下滚动,右下角会出现一个方形的多彩按钮。</span>
        <BackTop aria-label="回到顶部" colorful shape="square" size="large" style="bottom: 100px">
            <template #icon>
                <IconArrowUp />
            </template>
        </BackTop>
    </div>
</template>

API Reference

Props

PropertyDescriptionTypeDefault
durationMilliseconds the scroll to the top takes. The click event is throttled by this interval.numbersemiDefault("BackTop", "duration", 450)
targetFunction returning the scrolled element, the window when unset. Returning null means the node is not available yet. Called on mount and again when the function itself changes. Template refs are assigned before the parent's mounted hook, so they can be read directly.Type(() => Window | HTMLElement | null)undefined
visibilityHeightScroll distance after which the BackTop appears.numbersemiDefault("BackTop", "visibilityHeight", 400)
badgeBadge props, same as FloatButton.FloatButtonBadgesemiGlobalDefault("BackTop", "badge")
colorfulColorful button, same as FloatButton. Falls back to the FloatButton default when unset.booleansemiGlobalDefault("BackTop", "colorful")
shapeShape: round or square. Falls back to the FloatButton default when unset."square" | "round"semiGlobalDefault("BackTop", "shape")
sizeSize: default, small or large. Falls back to the FloatButton default when unset."small" | "default" | "large"semiGlobalDefault("BackTop", "size")

Events

EventDescriptionCallback parameters
clickEmitted on click with the native click event. Throttled by duration, and emitted once the scroll has started.[event: MouseEvent]

Slots

SlotDescriptionScope parameters
defaultStatic content of the button, shown in place of the icon. Must not contain interactive elements.any
badgeCountCustom badge content, in place of badge.count.any
iconButton icon. Defaults to IconBackTop.any

Exposes

NameDescriptionType
None

class, style and native attributes (such as data-* and aria-*) are passed through attrs and land on the button. click is a declared event, so it shadows a native click listener on the root and is throttled by duration. When target reads a template ref, the ref is already assigned before the parent's mounted hook; a stable target function that returns the element only after mount is not rebound, so remount BackTop. The $el of the component ref is the <button> while visible and a placeholder comment node otherwise.

Accessibility

ARIA

  • BackTop renders a single native <button type="button">, which screen readers announce as a button
  • The accessible name comes from aria-label; without it, it comes from the button content when content is passed, and otherwise from the locale's default name ('Back to top' in English)
  • children (the default slot in Vue) is static content of the button and becomes part of its name; the button must not contain interactive elements

Keyboard and focus

  • Once visible, the button is reached with Tab and Shift + Tab, and Enter or Space scrolls to the top
  • A focus outline is shown on keyboard focus

Design Tokens

These are the component's internal Sass variables and cannot be overridden. To customize styles, override the --semi-* CSS variables (see the Design Token page) or write unlayered CSS rules.
Sass variableValueUsage
$spacing-backtop-right100px返回顶部距离右侧距离
$spacing-backtop-bottom50px返回顶部距离底侧距离