FloatButton 悬浮按钮
固定在窗口边缘、提供快速操作的悬浮按钮,也支持带描述、徽标和展开菜单的按钮组。
何时使用
- 页面需要一个不随内容滚动、始终可达的帮助、反馈、创建或快捷入口。
- 多个低频操作需要收纳到一个角落时,用
FloatButtonGroup。 - 需要回到滚动容器顶部时,用
BackTop;不要自己用 FloatButton 模拟滚动逻辑。 - 只在当前内容流中操作的按钮,应使用 Button 按钮,不要把所有操作都做成悬浮按钮。
在线体验
gallery 展示右下角悬浮按钮、方形组、带描述与徽标的按钮组,以及点击触发展开和常驻展开两种组模式。
基本用法
FloatButton 默认使用 icon.Plus,位于窗口右下角,距离右侧 24px、底部 48px。用 Icon 和 Tooltip 为图标操作提供语义。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
)
func floatButtonPage(c *ui.Context) {
if ui.FloatButton(c).Icon(icon.Question).Tooltip("帮助").Primary().Clicked() {
ui.Message(c, "success", "已打开帮助")
}
}浮动按钮的点击结果只对当前帧有效;打开面板、计数或跳转等业务状态应由页面变量保存。
常见用法
位置与主色
Right 和 Bottom 设置到窗口逻辑末端的距离;Primary 使用品牌色,Square 改为圆角方形。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
)
func floatPositionPage(c *ui.Context) {
ui.FloatButton(c).Icon(icon.Question).Primary().Tooltip("帮助").Right(24).Bottom(40)
ui.FloatButton(c).Icon(icon.Setting).Square().Tooltip("设置").Right(88).Bottom(40)
}在 RTL 作用域中,Right 表示逻辑末端,组件会把悬浮位置镜像到对应一侧。
描述与徽标
Description 在图标下方显示短标题并自动使用方形外观;Badge(n) 显示数量,传入负数时显示点徽标。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
)
func floatStatusPage(c *ui.Context) {
ui.FloatButton(c).Icon(icon.Message).Description("客服").Badge(5).Right(24).Bottom(40)
ui.FloatButton(c).Icon(icon.Bell).Badge(-1).Tooltip("有新通知").Right(88).Bottom(40)
}常驻按钮组
把多个 FloatButton 放进 FloatButtonGroup.Body 会在同一位置垂直排列。组上的 Square 和 Primary 会影响组的外观。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
)
func floatGroupPage(c *ui.Context) {
ui.FloatButtonGroup(c).Square().Position(120, 40).Body(func() {
ui.FloatButton(c).Icon(icon.Question).A11yLabel("帮助")
ui.FloatButton(c).Icon(icon.Message).A11yLabel("消息").Badge(-1)
ui.FloatButton(c).Icon(icon.Setting).A11yLabel("设置")
})
}点击触发展开
Trigger("click") 将成员收纳到触发按钮后;Open(&open) 把展开状态交给调用方,适合在点击成员后执行操作或与其他面板联动。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
)
var floatOpen bool
func floatTriggerPage(c *ui.Context) {
ui.FloatButtonGroup(c).Trigger("click").Open(&floatOpen).Primary().Position(200, 40).Body(func() {
if ui.FloatButton(c).Icon(icon.Message).A11yLabel("消息").Clicked() {
floatOpen = false
}
ui.FloatButton(c).Icon(icon.Edit).A11yLabel("编辑")
})
}默认属性
ConfigProvider.Defaults("FloatButton", ...) 可统一设置形状和类型;单个按钮的 Square 与 Primary 会覆盖作用域默认值。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
)
func floatDefaultsPage(c *ui.Context) {
ui.ConfigProvider(c).Defaults("FloatButton", ui.Props{
"shape": "square",
"type": "primary",
}).Body(func() {
ui.FloatButton(c).Icon(icon.Plus).A11yLabel("新增")
})
}主题与样式
- Ant Design 主题使用圆形默认外观、阴影和品牌色主按钮;Element Plus 按悬浮按钮的圆角方形语义展示;shadcn 采用页面背景上的浮动卡片样式。
- 组件令牌位于
theme.Components.FloatButton(theme.FloatButtonToken),包括Size、DescriptionHeight、IconSize、DescriptionIconSize、Radius、Shadow、Bg、HoverBg、PrimaryBg和PrimaryHoverBg。 ConfigProvider.Defaults("FloatButton", ...)支持shape("circle"/"square")和type("default"/"primary")。
注意事项
- 状态放在哪:
Open(&open)使用调用方持有的 bool;如果不绑定,组会在当前作用域内部保存展开状态。业务弹窗、未读数和点击结果仍应由页面状态保存。 - 无障碍:图标按钮无可见文字时必须调用
A11yLabel或Tooltip;组内成员也要分别命名。键盘用户可以聚焦按钮并使用 Enter 或空格激活。 - 层级与遮挡:悬浮按钮位于窗口浮层,不会随普通内容滚动;不要把它放在需要被滚动容器裁剪的布局假设中。
- RTL:位置使用逻辑末端,徽标和提示方向也随作用域方向镜像。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.FloatButton
func FloatButton(c *Context) *FloatButtonWFloatButton adds a floating action button (default bottom-right, 24px from the right and 48px from the bottom). Inside a FloatButtonGroup it becomes a member of the group.
返回 *FloatButtonW,可链式调用的方法见 FloatButtonW 方法。
ui.FloatButtonGroup
func FloatButtonGroup(c *Context) *FloatButtonGroupWFloatButtonGroup starts a group; chain options, then Body with FloatButton calls.
返回 *FloatButtonGroupW,可链式调用的方法见 FloatButtonGroupW 方法。
ui.BackTop
func BackTop(c *Context, target ID) *BackTopWBackTop adds the button for the scroll container target (ScrollBox.ID()); it appears once scrolled past VisibilityHeight (antd 400, Element 200: FloatButtonToken.BackTop*) and scrolls back smoothly in Duration (450ms, antd duration). Call Show. It is antd's FloatButton.BackTop: Icon, Description, Primary and Square style it like a FloatButton.
返回 *BackTopW,可链式调用的方法见 BackTopW 方法。
FloatButtonW 方法
ui.FloatButtonW:FloatButtonW is a button floating above the page at a window corner.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name of an icon-only button without a tooltip. | A11yLabel(s string) *FloatButtonW |
Badge | Badge shows a count (dot when n < 0). | Badge(n int) *FloatButtonW |
Bottom | Bottom sets the distance from the bottom window edge. | Bottom(px float32) *FloatButtonW |
Clicked | Clicked reports activation. | Clicked() bool |
Description | Description shows a short caption below the icon (square shape). | Description(s string) *FloatButtonW |
ID | ID returns the widget ID. | ID() ID |
Icon | Icon sets the icon. | Icon(ic *icon.Icon) *FloatButtonW |
Primary | Primary uses the brand color. | Primary() *FloatButtonW |
Right | Right sets the distance from the right window edge (the end edge: the left one in a right-to-left scope). | Right(px float32) *FloatButtonW |
Square | Square uses a rounded square instead of a circle. | Square() *FloatButtonW |
Tooltip | Tooltip shows a tip on hover, towards the page (left; right in RTL). | Tooltip(s string) *FloatButtonW |
FloatButtonGroupW 方法
ui.FloatButtonGroupW:FloatButtonGroupW stacks float buttons; with a trigger it collapses them behind a menu button.
| 方法 | 说明 | 签名 |
|---|---|---|
Body | Body builds the members. | Body(fn func()) *FloatButtonGroupW |
Icon | Icon sets the menu button icon (default: a plus that turns into a cross). | Icon(ic *icon.Icon) *FloatButtonGroupW |
IsOpen | IsOpen reports whether the members are shown. | IsOpen() bool |
Open | Open binds the open state of a triggered group. | Open(open *bool) *FloatButtonGroupW |
Position | Position sets the distance from the right and bottom window edges. | Position(right, bottom float32) *FloatButtonGroupW |
Primary | Primary colors the menu button. | Primary() *FloatButtonGroupW |
Square | Square joins the buttons in one rounded card with separators. | Square() *FloatButtonGroupW |
Trigger | Trigger collapses the group behind a menu button opened by "click" or "hover". | Trigger(t string) *FloatButtonGroupW |
BackTopW 方法
ui.BackTopW:BackTopW is the "back to top" floating button of a scroll container.
| 方法 | 说明 | 签名 |
|---|---|---|
Clicked | Clicked reports a click this frame. | Clicked() bool |
Content | Content replaces the default icon with custom content (Element slot). | Content(fn func()) *BackTopW |
Description | Description shows a text under the icon in a square button (antd 6 content, description deprecated). | Description(s string) *BackTopW |
Duration | Duration sets the scroll animation time. | Duration(d time.Duration) *BackTopW |
Icon | Icon replaces the default icon (antd icon). | Icon(ic *icon.Icon) *BackTopW |
Position | Position sets the distance from the right and bottom window edges. | Position(right, bottom float32) *BackTopW |
Primary | Primary uses the primary button type (antd type="primary"). | Primary() *BackTopW |
Show | Show builds the button. | Show() *BackTopW |
Square | Square uses the square shape (antd shape="square"). | Square() *BackTopW |
VisibilityHeight | VisibilityHeight sets the scroll distance after which it shows. | VisibilityHeight(px float32) *BackTopW |