Skip to content

Slider 滑块

通过拖动滑块在一个固定区间内进行选择。

基础用法

在拖动滑块时,显示当前值。

当前值: 0
vue
<template>
  <x-slider v-model="value" />
</template>

<script setup>
import { ref } from 'vue'
const value = ref(0)
</script>

自定义初始值

当前值: 50
vue
<template>
  <x-slider v-model="value" />
</template>

<script setup>
import { ref } from 'vue'
const value = ref(50)
</script>

隐藏 Tooltip

设置 show-tooltipfalse 隐藏 Tooltip。

vue
<template>
  <x-slider v-model="value" :show-tooltip="false" />
</template>

格式化 Tooltip

使用 format-tooltip 格式化 Tooltip 内容。

当前值: 30%
vue
<template>
  <x-slider v-model="value" :format-tooltip="formatTooltip" />
</template>

<script setup>
const formatTooltip = (val) => {
  return val + '%'
}
</script>

禁用状态

设置 disabled 属性禁用滑块。

vue
<template>
  <x-slider v-model="value" disabled />
</template>

离散值

设置 step 属性设置步长,使用 show-stops 显示间断点。

不显示间断点

当前值: 0(步长: 10)
vue
<template>
  <x-slider v-model="value" :step="10" />
</template>

显示间断点

当前值: 36
vue
<template>
  <x-slider v-model="value" :step="10" show-stops />
</template>

竖向模式

设置 vertical 属性启用竖向模式,使用 height 设置高度。

当前值: 0
vue
<template>
  <x-slider v-model="value" vertical height="200px" />
</template>

带有标记

使用 marks 属性在滑块上显示标记。

0°C
8°C
37°C
50°C
100°C
vue
<template>
  <x-slider v-model="value" :marks="marks" />
</template>

<script setup>
const marks = {
  0: '0°C',
  37: '37°C',
  50: {
    style: {
      color: '#f56c6c'
    },
    label: '50°C'
  },
  100: '100°C'
}
</script>

API

Props

属性说明类型默认值
modelValue绑定值number0
min最小值number0
max最大值number100
step步长number1
disabled是否禁用booleanfalse
showTooltip是否显示 Tooltipbooleantrue
formatTooltip格式化 Tooltip 内容(value: number) => string | number
showStops是否显示间断点booleanfalse
vertical是否竖向模式booleanfalse
height滑块高度(竖向模式时有效)string | number'200px'
marks标记Record<number, string | { style?: object; label?: string }>

Events

事件名说明回调参数
update:modelValue值变化时触发(value: number)
change值改变时触发(鼠标释放时)(value: number)
input拖拽时触发(value: number)

基于 MIT 许可发布