Skip to content

Slider 滑块 ​

在指定数值范围内拖动选择一个值,适合音量、缩放、亮度或阈值设置。

在线体验 ​

拖动滑块,或聚焦画布后按左右方向键,观察当前值。 工具栏支持 Ant Design、Element Plus、shadcn 以及深浅色切换;重置会恢复初始状态。

Slider 滑块 · 在线体验真实 Go 组件
在独立页面打开 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
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 里这个组件的全部示例(基础、进阶与组合场景),可切换场景、主题和深浅色。

Slider 范围 / 刻度 / 竖向 / 提示 / 输入框 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

实现与更多示例 ​

全部示例代码 ​

以下是上方在线演示运行的 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 ​

go
func Slider(c *Context, val *float64, min, max float64) *SliderW

Slider 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 ​

go
func SliderRange(c *Context, lo, hi *float64, min, max float64) *SliderW

SliderRange 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.

方法说明签名
A11yLabelA11yLabel sets the accessible name (inside a FormItem the item label is used).A11yLabel(s string) *SliderW
AllowCrossAllowCross 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
ChangeCompleteChangeComplete 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
ChangedChanged reports a value change this frame.Changed() bool
DisabledDisabled turns the slider off.Disabled(on bool) *SliderW
DotsDots draws a dot at every step (Ant dots, Element show-stops).Dots() *SliderW
IncludedIncluded 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
KeyboardKeyboard turns the arrow / Page / Home / End keys on (the default) or off (antd keyboard); the handles stay focusable.Keyboard(on bool) *SliderW
LayoutLayout applies layout options to the slider row.Layout(opts ...layout.Option) *SliderW
MarksMarks labels values on the rail (a dot is drawn at each mark).Marks(m map[float64]string) *SliderW
PushablePushable 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
RangeRange turns the slider into a range: *val is the start, *hi the end.Range(hi *float64) *SliderW
ReverseReverse runs the slider the other way (antd reverse): max at the inline start (at the bottom when vertical).Reverse() *SliderW
StepStep sets the value granularity (0: continuous).Step(s float64) *SliderW
TooltipTooltip sets the tooltip formatter of the handle value (nil: the plain number).Tooltip(format func(float64) string) *SliderW
TooltipOpenTooltipOpen always shows (true) or never shows (false) the tooltips.TooltipOpen(on bool) *SliderW
VerticalVertical lays the slider out vertically with the given height (min at the bottom).Vertical(height float32) *SliderW
WithInputWithInput adds InputNumbers bound to the values (Element show-input).WithInput() *SliderW

以 MIT 许可发布