跳转到内容

FloatButton

固定在页面上的浮动操作按钮,用于快速访问常用操作。

import { FloatButton } from 'asterui'

基础

简单的浮动操作按钮。

import { FloatButton } from 'asterui'

function App() {
  return (
      <div className="relative h-32 bg-base-200 rounded-lg">
        <FloatButton
          icon={<span className="text-lg">+</span>}
          onClick={() => alert('Button clicked!')}
          className="!absolute !right-4 !bottom-4"
        />
      </div>
    )
}

export default App

带提示

悬停时显示提示的浮动按钮。

import { FloatButton } from 'asterui'

function App() {
  return (
      <div className="relative h-32 bg-base-200 rounded-lg">
        <FloatButton
          icon={<span className="text-lg">+</span>}
          tooltip="Add new item"
          type="primary"
          onClick={() => alert('Add clicked!')}
          className="!absolute !right-4 !bottom-4"
        />
      </div>
    )
}

export default App

类型

默认和主要按钮类型。

import { FloatButton } from 'asterui'

function App() {
  return (
      <div className="relative h-32 bg-base-200 rounded-lg">
        <FloatButton
          icon={<span className="text-lg">+</span>}
          type="default"
          className="!absolute !right-20 !bottom-4"
        />
        <FloatButton
          icon={<span className="text-lg">♥</span>}
          type="primary"
          className="!absolute !right-4 !bottom-4"
        />
      </div>
    )
}

export default App

形状

圆形和方形按钮形状。

import { FloatButton } from 'asterui'

function App() {
  return (
      <div className="relative h-32 bg-base-200 rounded-lg">
        <FloatButton
          icon={<span className="text-lg">+</span>}
          shape="circle"
          className="!absolute !right-20 !bottom-4"
        />
        <FloatButton
          icon={<span className="text-lg">+</span>}
          shape="square"
          className="!absolute !right-4 !bottom-4"
        />
      </div>
    )
}

export default App

带徽章

带通知徽章的浮动按钮。

5
import { FloatButton } from 'asterui'

function App() {
  return (
      <div className="relative h-32 bg-base-200 rounded-lg">
        <FloatButton
          icon={<span className="text-lg">💬</span>}
          badge={5}
          onClick={() => alert('You have 5 unread messages')}
          className="!absolute !right-4 !bottom-4"
        />
      </div>
    )
}

export default App

浮动按钮组

点击时展开的浮动按钮组。

import { FloatButton } from 'asterui'

function App() {
  return (
      <div className="relative h-48 bg-base-200 rounded-lg">
        <FloatButton.Group
          icon={<span className="text-lg">+</span>}
          className="!absolute !right-4 !bottom-4"
        >
          <FloatButton
            icon={<span>?</span>}
            onClick={() => alert('Help section')}
          />
          <FloatButton
            icon={<span>💬</span>}
            onClick={() => alert('Contact support')}
          />
          <FloatButton
            icon={<span>↻</span>}
            onClick={() => alert('Refreshing...')}
          />
        </FloatButton.Group>
      </div>
    )
}

export default App

回到顶部

向下滚动后出现的滚动到顶部按钮。

BackTop button appears after scrolling down 400px

import { FloatButton } from 'asterui'

function App() {
  return (
      <div className="relative h-32 bg-base-200 rounded-lg flex items-center justify-center text-base-content/60">
        <p>BackTop button appears after scrolling down 400px</p>
      </div>
    )
}

export default App
属性描述类型默认值
icon在按钮中显示的图标React.ReactNode-
description图标下方的描述文本React.ReactNode-
type按钮类型/颜色'default' | 'primary''default'
shape按钮形状'circle' | 'square''circle'
tooltip悬停时显示的提示文本string-
tooltipPlacement提示位置'left' | 'right' | 'top' | 'bottom''left'
badge要显示的徽章内容number | React.ReactNode-
href链接 URL(渲染为锚点)string-
target链接目标string-
onClick点击事件处理器() => void-
className额外的 CSS 类名string-
data-testid用于测试的测试 IDstring-
属性描述类型默认值
children要分组的 FloatButton 组件React.ReactNode-
flower以四分之一圆(径向)布局排列按钮booleanfalse
shape子按钮的形状'circle' | 'square''circle'
icon主触发按钮图标React.ReactNode-
mainAction主操作按钮图标(打开时替换触发器)React.ReactNode-
onMainAction主操作按钮的点击处理器() => void-
showClose打开时显示关闭按钮booleanfalse
type按钮类型/颜色'default' | 'primary''default'
position屏幕上的位置'bottom-right' | 'bottom-left' | 'top-right' | 'top-left''bottom-right'
offset距边缘的距离(像素)number24
data-testid用于测试的测试 IDstring-
属性描述类型默认值
visibilityHeight按钮出现前的滚动距离(像素)number400
target滚动目标元素() => HTMLElement | Window-
onClick点击事件处理器(在滚动前调用)() => void-
icon自定义图标React.ReactNode-
duration滚动动画持续时间(毫秒)number450
position屏幕上的位置'bottom-right' | 'bottom-left''bottom-right'
offset距边缘的距离(像素)number24
data-testid用于测试的测试 IDstring-
  • 使用原生 <button> 元素以获得正确的键盘支持
  • 支持 href 属性以渲染为锚点以实现链接功能
  • 提示在存在时为屏幕阅读器提供上下文
  • 键盘导航的焦点状态清晰可见
  • 组展开支持键盘访问
  • BackTop 按钮根据滚动位置出现/消失