跳转到内容
Nacre
组件配色
EN

滑条 Slider

stableSlider在画廊运行
Slider
在浏览器里运行真实组件。

在一段范围里拖出一个连续数值:音量、录音播放进度。拖动中走 onChange,seek 这类只在松手时做的走 onChangeEnd。

状态:stable · 组件:Slider

import { Slider } from 'nacre'
import { Text, View, signal } from 'nsh'
import { Slider, TEXT_SIZE } from 'nacre'
export function SliderUsage() {
const progress = signal(40)
return (
<View class="flex-row flex-wrap items-center gap-3">
<View class="w-[260px] max-w-full">
<Slider label="进度" value={progress()} max={100} onChange={(value) => progress.set(value)} />
</View>
<Text class={`${TEXT_SIZE.small} text-muted-foreground tabular-nums`}>{String(progress())}</Text>
</View>
)
}

拖动时只更新显示的时间,松手(onChangeEnd)才跳转播放位置,避免拖动中反复 seek。

import { Text, View, signal } from 'nsh'
import { Slider, TEXT_SIZE } from 'nacre'
const DURATION = 185
const clock = (seconds: number) => `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`
export function SliderSeek() {
const dragging = signal(12)
const playing = signal(12)
return (
<View class="w-[300px] max-w-full gap-1">
<Slider
label="通话录音"
value={dragging()}
max={DURATION}
onChange={(value) => dragging.set(value)}
onChangeEnd={(value) => playing.set(value)}
/>
<View class="flex-row justify-between">
<Text class={`${TEXT_SIZE.caption} text-muted-foreground tabular-nums`}>{`${clock(dragging())} / ${clock(DURATION)}`}</Text>
<Text class={`${TEXT_SIZE.caption} text-muted-foreground tabular-nums`}>{`$播放位置 ${clock(playing())}`}</Text>
</View>
</View>
)
}

min / max / step 定范围与步长,键盘每按一次走一个 step。

手机:min / max / step 定范围与步长,拖动时按 step 吸附。

import { Text, View, signal } from 'nsh'
import { Slider, TEXT_SIZE } from 'nacre'
export function SliderStep() {
const volume = signal(60)
return (
<View class="flex-row flex-wrap items-center gap-3">
<View class="w-[200px] max-w-full">
<Slider label="提示音音量" value={volume()} min={0} max={100} step={10} onChange={(value) => volume.set(value)} />
</View>
<Text class={`${TEXT_SIZE.small} text-muted-foreground tabular-nums`}>{`${volume()}%`}</Text>
</View>
)
}

不响应拖动与键盘,降到 40% 不透明度。

手机:不响应拖动,降到 40% 不透明度。

import { View } from 'nsh'
import { Slider } from 'nacre'
export function SliderDisabled() {
return (
<View class="w-[260px] max-w-full">
<Slider label="禁用" value={30} max={100} disabled />
</View>
)
}
键 行为
Tab 聚焦到滑块
← → / ↑ ↓ 按 step 减小 / 增大
Home / End 跳到最小 / 最大
  • label 必填,是滑块的读屏名;读屏把它读成可调节控件并报当前值。
  • 旁边显示当前值的文字,不只靠滑块位置。

该用

  • seek 类用 onChangeEnd
  • 旁边显示当前值

不该用

  • 要精确输入的数值(用输入框)
  • 只有几个离散档位(用分段)
属性 类型 默认 说明
value * number —
min number 0
max * number —
step number 1
label * string —
disabled boolean —
testID string —
class string 'w-full'
onChange (value: number) => void —
onChangeEnd (value: number) => void — 拖完松手(seek 这类只在结束时做的)

* 必填