Slider 滑块

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

基础用法

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

通过设置绑定值自定义滑块的初始值

Slider 滑块 - 图1

  1. <template>
  2. <div class="slider-demo-block">
  3. <span class="demonstration">Default value</span>
  4. <el-slider v-model="value1" />
  5. </div>
  6. <div class="slider-demo-block">
  7. <span class="demonstration">Customized initial value</span>
  8. <el-slider v-model="value2" />
  9. </div>
  10. <div class="slider-demo-block">
  11. <span class="demonstration">Hide Tooltip</span>
  12. <el-slider v-model="value3" :show-tooltip="false" />
  13. </div>
  14. <div class="slider-demo-block">
  15. <span class="demonstration">Format Tooltip</span>
  16. <el-slider v-model="value4" :format-tooltip="formatTooltip" />
  17. </div>
  18. <div class="slider-demo-block">
  19. <span class="demonstration">Disabled</span>
  20. <el-slider v-model="value5" disabled />
  21. </div>
  22. </template>
  23. <script lang="ts" setup>
  24. import { ref } from 'vue'
  25. const value1 = ref(0)
  26. const value2 = ref(0)
  27. const value3 = ref(0)
  28. const value4 = ref(0)
  29. const value5 = ref(0)
  30. const formatTooltip = (val: number) => {
  31. return val / 100
  32. }
  33. </script>
  34. <style scoped>
  35. .slider-demo-block {
  36. display: flex;
  37. align-items: center;
  38. }
  39. .slider-demo-block .el-slider {
  40. margin-top: 0;
  41. margin-left: 12px;
  42. }
  43. .slider-demo-block .demonstration {
  44. font-size: 14px;
  45. color: var(--el-text-color-secondary);
  46. line-height: 44px;
  47. flex: 1;
  48. overflow: hidden;
  49. text-overflow: ellipsis;
  50. white-space: nowrap;
  51. margin-bottom: 0;
  52. }
  53. .slider-demo-block .demonstration + .el-slider {
  54. flex: 0 0 70%;
  55. }
  56. </style>

离散值

选项可以是离散的

改变step的值可以改变步长, 通过设置 show-stops 属性可以显示间断点

Slider 滑块 - 图2

  1. <template>
  2. <div class="slider-demo-block">
  3. <span class="demonstration">Breakpoints not displayed</span>
  4. <el-slider v-model="value1" :step="10" />
  5. </div>
  6. <div class="slider-demo-block">
  7. <span class="demonstration">Breakpoints displayed</span>
  8. <el-slider v-model="value2" :step="10" show-stops />
  9. </div>
  10. </template>
  11. <script lang="ts" setup>
  12. import { ref } from 'vue'
  13. const value1 = ref(0)
  14. const value2 = ref(0)
  15. </script>
  16. <style scoped>
  17. .slider-demo-block {
  18. display: flex;
  19. align-items: center;
  20. }
  21. .slider-demo-block .el-slider {
  22. margin-top: 0;
  23. margin-left: 12px;
  24. }
  25. .slider-demo-block .demonstration {
  26. font-size: 14px;
  27. color: var(--el-text-color-secondary);
  28. line-height: 44px;
  29. flex: 1;
  30. overflow: hidden;
  31. text-overflow: ellipsis;
  32. white-space: nowrap;
  33. margin-bottom: 0;
  34. }
  35. .slider-demo-block .demonstration + .el-slider {
  36. flex: 0 0 70%;
  37. }
  38. </style>

带有输入框的滑块

通过输入框设置精确数值

设置 show-input 属性会在右侧显示一个输入框

Slider 滑块 - 图3

  1. <template>
  2. <div class="slider-demo-block">
  3. <el-slider v-model="value" show-input />
  4. </div>
  5. </template>
  6. <script lang="ts" setup>
  7. import { ref } from 'vue'
  8. const value = ref(0)
  9. </script>
  10. <style scoped>
  11. .slider-demo-block {
  12. display: flex;
  13. align-items: center;
  14. }
  15. .slider-demo-block .el-slider {
  16. margin-top: 0;
  17. margin-left: 12px;
  18. }
  19. </style>

不同尺寸

Slider 滑块 - 图4

  1. <template>
  2. <el-slider v-model="value" show-input size="large" />
  3. <el-slider v-model="value" show-input />
  4. <el-slider v-model="value" show-input size="small" />
  5. </template>
  6. <script lang="ts" setup>
  7. import { ref } from 'vue'
  8. const value = ref(0)
  9. </script>
  10. <style scoped>
  11. .el-slider {
  12. margin-top: 20px;
  13. }
  14. .el-slider:first-child {
  15. margin-top: 0;
  16. }
  17. </style>
  18. <style scoped>
  19. .slider-demo-block {
  20. display: flex;
  21. align-items: center;
  22. }
  23. .slider-demo-block .el-slider {
  24. margin-top: 0;
  25. margin-left: 12px;
  26. }
  27. </style>

位置

您可以自定义 Tooltip 提示的位置。

Slider 滑块 - 图5

  1. <template>
  2. <div class="slider-demo-block">
  3. <el-slider v-model="value1" />
  4. </div>
  5. <div class="slider-demo-block">
  6. <el-slider v-model="value2" placement="bottom" />
  7. </div>
  8. <div class="slider-demo-block">
  9. <el-slider v-model="value3" placement="right" />
  10. </div>
  11. <div class="slider-demo-block">
  12. <el-slider v-model="value4" placement="left" />
  13. </div>
  14. </template>
  15. <script lang="ts" setup>
  16. import { ref } from 'vue'
  17. const value1 = ref(0)
  18. const value2 = ref(0)
  19. const value3 = ref(0)
  20. const value4 = ref(0)
  21. </script>
  22. <style scoped>
  23. .slider-demo-block {
  24. display: flex;
  25. align-items: center;
  26. }
  27. .slider-demo-block .el-slider {
  28. margin-top: 0;
  29. margin-left: 12px;
  30. }
  31. </style>

范围选择

支持选择某一数值范围

配置 range 属性以激活范围选择模式,该属性的绑定值是一个数组,由最小边界值和最大边界值组成。

Slider 滑块 - 图6

  1. <template>
  2. <div class="slider-demo-block">
  3. <el-slider v-model="value" range show-stops :max="10" />
  4. </div>
  5. </template>
  6. <script lang="ts" setup>
  7. import { ref } from 'vue'
  8. const value = ref([4, 8])
  9. </script>
  10. <style scoped>
  11. .slider-demo-block {
  12. display: flex;
  13. align-items: center;
  14. }
  15. .slider-demo-block .el-slider {
  16. margin-top: 0;
  17. margin-left: 12px;
  18. }
  19. </style>

垂直模式

配置 vertical 属性为 true 启用垂直模式。 在垂直模式下,必须设置 height 属性。

Slider 滑块 - 图7

  1. <template>
  2. <div class="slider-demo-block">
  3. <el-slider v-model="value" vertical height="200px" />
  4. </div>
  5. </template>
  6. <script lang="ts" setup>
  7. import { ref } from 'vue'
  8. const value = ref(0)
  9. </script>
  10. <style scoped>
  11. .slider-demo-block {
  12. display: flex;
  13. align-items: center;
  14. }
  15. .slider-demo-block .el-slider {
  16. margin-top: 0;
  17. margin-left: 12px;
  18. }
  19. </style>

显示标记

设置 marks 属性可以展示标记

Slider 滑块 - 图8

  1. <template>
  2. <div class="slider-demo-block">
  3. <el-slider v-model="value" range :marks="marks" />
  4. </div>
  5. </template>
  6. <script lang="ts" setup>
  7. import { reactive, ref } from 'vue'
  8. import type { CSSProperties } from 'vue'
  9. interface Mark {
  10. style: CSSProperties
  11. label: string
  12. }
  13. type Marks = Record<number, Mark | string>
  14. const value = ref([30, 60])
  15. const marks = reactive<Marks>({
  16. 0: '0°C',
  17. 8: '8°C',
  18. 37: '37°C',
  19. 50: {
  20. style: {
  21. color: '#1989FA',
  22. },
  23. label: '50%',
  24. },
  25. })
  26. </script>
  27. <style scoped>
  28. .slider-demo-block {
  29. display: flex;
  30. align-items: center;
  31. }
  32. .slider-demo-block .el-slider {
  33. margin-top: 0;
  34. margin-left: 12px;
  35. }
  36. </style>

属性

属性描述类型可选值默认值
model-value / v-model选中项绑定值number0
min最小值number0
max最大值number100
disabled是否禁用booleanfalse
step步长number1
show-input是否显示输入框,仅在非范围选择时有效booleanfalse
show-input-controls在显示输入框的情况下,是否显示输入框的控制按钮booleantrue
size滑块的大小stringlarge / default / smalldefault
input-size输入框的大小,如果设置了 size 属性,默认值自动取 sizestringlarge / default / smalldefault
show-stops是否显示间断点booleanfalse
show-tooltip是否显示提示信息booleantrue
format-tooltip格式化提示信息function(value)
range是否开启选择范围booleanfalse
vertical垂直模式booleanfalse
height滑块高度,垂直模式必填string
label屏幕阅读器标签string
range-start-labelrange 为true时,屏幕阅读器标签开始的标记string
range-end-labelrange 为true时,屏幕阅读器标签结尾的标记string
format-value-text显示屏幕阅读器的 aria-valuenow 属性的格式function(value)
debounce输入时的去抖延迟,毫秒,仅在 show-input 等于 true 时有效number300
tooltip-classtooltip 的自定义类名string
placementTooltip 出现的位置stringtop/top-start/top-end/bottom/bottom-start/bottom-end/left/left-start/left-end/right/right-start/right-endtop
marks标记, 对象的键类型需为 number 且对象的值必须在 [min, max]闭区间内,每个标记均可自定义样式object
validate-event输入时是否触发表单的校验boolean-true

事件

事件名说明参数
change值改变时触发(使用鼠标拖曳时,只在松开鼠标后触发)val,新状态的值
input数据改变时触发(使用鼠标拖曳时,活动过程实时触发)val,改变后的值

源代码

组件 Slider 滑块 - 图9 文档 Slider 滑块 - 图10

贡献者

Slider 滑块 - 图11 三咲智子

Slider 滑块 - 图12 云游君

Slider 滑块 - 图13 jeremywu

Slider 滑块 - 图14 kooriookami

Slider 滑块 - 图15 zz

Slider 滑块 - 图16 Delyan Haralanov

Slider 滑块 - 图17 renovate[bot]

Slider 滑块 - 图18 qiang

Slider 滑块 - 图19 류한경

Slider 滑块 - 图20 bqy_fe

Slider 滑块 - 图21 opengraphica

Slider 滑块 - 图22 C.Y.Kun

Slider 滑块 - 图23 Xc

Slider 滑块 - 图24 Hefty

Slider 滑块 - 图25 blackie

Slider 滑块 - 图26 msidolphin

Slider 滑块 - 图27 spx

Slider 滑块 - 图28 Aex

Slider 滑块 - 图29 on the field of hope

Slider 滑块 - 图30 Ryan2128

Slider 滑块 - 图31 Simona

Slider 滑块 - 图32 Hades-li