Slider 滑块
在指定数值范围内拖动选择一个值,适合音量、缩放、亮度或阈值设置。
在线体验
拖动滑块,或聚焦画布后按左右方向键,观察当前值。 工具栏支持 Ant Design、Element Plus、shadcn 以及深浅色切换;重置会恢复初始状态。
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"github.com/tangoui/tango/ui"
"fmt"
)
var level float64 = 35
func page(c *ui.Context) {
ui.Slider(c, &level, 0, 100).Step(1)
ui.Text(c, fmt.Sprintf("Value: %.0f", level))
}常用参数与方法
| 参数 / 方法 | 说明 |
|---|---|
val *float64 | 当前数值,由应用持有 |
min, max float64 | 允许的数值区间 |
Step(float64) | 步长 |
Marks(map[float64]string) | 刻度标签 |
WithInput() | 组合数值输入框 |
Disabled(bool) | 禁用滑块 |
Changed() bool | 当前帧是否发生数值变化 |
使用说明
范围选择使用 ui.SliderRange(c, &lo, &hi, min, max)。键盘方向键可细调数值,精确输入可使用 WithInput。
更多场景
官方 gallery 里这个组件的全部示例(基础、进阶与组合场景),可切换场景、主题和深浅色。
实现与更多示例
全部示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
Slider 范围 / 刻度 / 竖向 / 提示 / 输入框
gallery 演示 SliderAdvanced 的 Render 函数,源码 page_p2b_slider.go#L38。
加载中…
完整 API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Slider
func Slider(c *Context, val *float64, min, max float64) *SliderWSlider adds a slider bound to *val in [min,max] (step 1).
ui.Slider(c, &v, 0, 100).Marks(map[float64]string{0: "0°C", 100: "100°C"}).Tooltip(nil)
ui.Slider(c, &v, 0, 100).Vertical(200)
ui.Slider(c, &v, 0, 10).Step(0.5).WithInput()
返回 *SliderW,可链式调用的方法见 SliderW 方法。
ui.SliderRange
func SliderRange(c *Context, lo, hi *float64, min, max float64) *SliderWSliderRange adds a two-handle slider bound to *lo and *hi (Ant range, Element range).
返回 *SliderW,可链式调用的方法见 SliderW 方法。
SliderW 方法
ui.SliderW:SliderW is a slider; chain modifiers after Slider / SliderRange.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (inside a FormItem the item label is used). | A11yLabel(s string) *SliderW |
AllowCross | AllowCross sets whether a dragged range handle may pass the other one (they swap values and the dragged handle continues as the other end). | AllowCross(on bool) *SliderW |
ChangeComplete | ChangeComplete reports the end of a change (antd onChangeComplete): the frame the pointer is released after dragging changed the value, or a keyboard / number box / assistive change. | ChangeComplete() bool |
Changed | Changed reports a value change this frame. | Changed() bool |
Disabled | Disabled turns the slider off. | Disabled(on bool) *SliderW |
Dots | Dots draws a dot at every step (Ant dots, Element show-stops). | Dots() *SliderW |
Included | Included sets whether the part from the start to the value (a range: between the handles) is filled (antd included, default true); false leaves the rail plain, for marks that are points rather than ranges. | Included(on bool) *SliderW |
Keyboard | Keyboard turns the arrow / Page / Home / End keys on (the default) or off (antd keyboard); the handles stay focusable. | Keyboard(on bool) *SliderW |
Layout | Layout applies layout options to the slider row. | Layout(opts ...layout.Option) *SliderW |
Marks | Marks labels values on the rail (a dot is drawn at each mark). | Marks(m map[float64]string) *SliderW |
Pushable | Pushable keeps range handles at least gap apart: moving one towards the other pushes it along (until it reaches the end of the slider). | Pushable(gap float64) *SliderW |
Range | Range turns the slider into a range: *val is the start, *hi the end. | Range(hi *float64) *SliderW |
Reverse | Reverse runs the slider the other way (antd reverse): max at the inline start (at the bottom when vertical). | Reverse() *SliderW |
Step | Step sets the value granularity (0: continuous). | Step(s float64) *SliderW |
Tooltip | Tooltip sets the tooltip formatter of the handle value (nil: the plain number). | Tooltip(format func(float64) string) *SliderW |
TooltipOpen | TooltipOpen always shows (true) or never shows (false) the tooltips. | TooltipOpen(on bool) *SliderW |
Vertical | Vertical lays the slider out vertically with the given height (min at the bottom). | Vertical(height float32) *SliderW |
WithInput | WithInput adds InputNumbers bound to the values (Element show-input). | WithInput() *SliderW |