滑块
QSlider 是用户指定最小值和最大值之间的数值的好方法,滑块还有一个焦点指示器(突出显示的滑块按钮),可以通过键盘调整滑块。
也看看另一个相似的组件:QRange。
QSlider API
QSlider API
props
37
slots
2
events
3
behavior
5
content
5
general
1
model
6
state
2
style
18
name
: String
说明
用于指定控件的名称;如果处理直接提交到 URL 的表单时很有用
snap
: Boolean
说明
根据有效值进行捕捉,而不是自由滑动;建议:与 'step' 属性一起使用
reverse
: Boolean
说明
逆向工作(改变方向)
vertical
: Boolean
说明
在垂直方向显示
label-always
: Boolean
说明
始终显示标签
用法
WARNING
您需要调整 QSlider 周围的空间,以便标签和标记标签不会与页面上的其他内容重叠。您可以为此使用 CSS 边距或填充。
标准
标准
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ standard }} (0 to 50)
</q-badge>
<q-slider v-model="standard" :min="0" :max="50"/>
<q-slider v-model="standard" :min="0" :max="50" color="green"/>
</div>
</template>
Copied to clipboard
Model: 2 (0 to 50)
垂直
垂直方向
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ standard }} (0 to 50)
</q-badge>
<div class="row justify-around">
<q-slider
v-model="standard"
:min="0"
:max="50"
vertical
label
switch-label-side
/>
<q-slider
v-model="standard"
:min="0"
:max="50"
color="green"
vertical
reverse
label-always
/>
</div>
</div>
</template>
Copied to clipboard
Model: 10 (0 to 50)
10
10
内部的最大/最小值 v2.4+
有时您需要将模型值限制为轨迹长度内的范围。您可以通过 inner-min
和 inner-max
属性来实现,前者需要大于等于 min
属性,后者需要小于等于 max
属性。
Inner min/max
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ value }} (0 to 50 w/ selection 10 to 35 or 15 to 40)
</q-badge>
<q-slider v-model="value" :min="0" :max="50" :inner-min="10" :inner-max="35" />
<q-slider v-model="value" :min="0" :max="50" color="green" :inner-min="15" :inner-max="40" />
</div>
</template>
Copied to clipboard
Model: 25 (0 to 50 w/ selection 10 to 35 or 15 to 40)
离散值
设置步长
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ basicModel }} (0 to 100, step 50)
</q-badge>
<q-slider
v-model="basicModel"
:step="50"
/>
<q-badge color="secondary">
Model: {{ redModel }} (0 to 100, step 25)
</q-badge>
<q-slider
v-model="redModel"
color="red"
:step="25"
/>
<q-badge color="secondary">
Model: {{ greenModel }} (0 to 5, step 1)
</q-badge>
<q-slider
v-model="greenModel"
color="green"
:min="0"
:step="1"
:max="5"
/>
</div>
</template>
Copied to clipboard
Model: 50 (0 to 100, step 50)
Model: 25 (0 to 100, step 25)
Model: 2 (0 to 5, step 1)
step
属性也可以是一个浮点数(或者数字 0 如果您需要无效小的精度)。
浮点数
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ smallStep }} (0.1 to 1.5, step 0.1)
</q-badge>
<q-slider
v-model="smallStep"
:min="0.1"
:max="1.5"
:step="0.1"
/>
<q-badge color="secondary">
Model: {{ xsmallStep }} (0.1 to 1, step 0.05)
</q-badge>
<q-slider
v-model="xsmallStep"
:min="0.1"
:max="1"
:step="0.05"
/>
<q-badge color="secondary">
Model: {{ zeroStep }} (0.0 to 10.5, step 0)
</q-badge>
<q-slider
v-model="zeroStep"
:min="0.0"
:max="10.5"
:step="0"
color="red"
/>
</div>
</template>
Copied to clipboard
Model: 0.3 (0.1 to 1.5, step 0.1)
Model: 0.53 (0.1 to 1, step 0.05)
Model: 0.5 (0.0 to 10.5, step 0)
Snap to steps
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ value }} (-20 to 20)
</q-badge>
<q-slider
v-model="value"
:min="-20"
:max="20"
:step="4"
snap
label
color="purple"
/>
</div>
</template>
Copied to clipboard
Model: 0 (-20 to 20)
0
带有标签
在下面的示例中,移动滑块以查看标签。
带有标签
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ value }} (-20 to 20, step 4)
</q-badge>
<q-slider
v-model="value"
:min="-20"
:max="20"
:step="4"
label
color="light-green"
/>
</div>
</template>
Copied to clipboard
Model: 0 (-20 to 20, step 4)
0
始终显示标签
Template
Script
All
<template>
<div class="q-pa-md q-pb-lg">
<q-badge color="secondary">
Model: {{ value }} (-20 to 20, step 4)
</q-badge>
<q-slider
v-model="value"
:min="-20"
:max="20"
:step="4"
label
label-always
color="light-green"
/>
<q-badge color="secondary">
Model: {{ value }} (-20 to 20, step 4)
</q-badge>
<q-slider
v-model="value"
:min="-20"
:max="20"
:step="4"
label
label-always
switch-label-side
color="red"
/>
</div>
</template>
Copied to clipboard
Model: 0 (-20 to 20, step 4)
0
Model: 0 (-20 to 20, step 4)
0
自定义标签值
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ value }} (-20 to 20, step 4)
</q-badge>
<q-slider
v-model="value"
:min="-20"
:max="20"
:step="4"
label
:label-value="value + 'px'"
label-always
color="purple"
/>
<q-badge color="secondary">
Model: {{ value }} (-20 to 20, step 4)
</q-badge>
<q-slider
v-model="value"
:min="-20"
:max="20"
:step="4"
label
:label-value="value + 'px'"
label-always
color="red"
/>
</div>
</template>
Copied to clipboard
Model: 0 (-20 to 20, step 4)
0px
Model: 0 (-20 to 20, step 4)
0px
下面的示例演示 QSlider 如何处理标签的定位,以使其始终水平地保持在 QSlide 的框内。
长标签
Template
Script
All
<template>
<div class="q-pa-md">
<q-slider
v-model="value"
:min="-20"
:max="20"
:step="4"
label
:label-value="'Value: ' + value + 'px'"
label-always
color="purple"
/>
</div>
</template>
Copied to clipboard
Value: 16px
标记
标记
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ basicModel }} (0 to 10)
</q-badge>
<q-slider
v-model="basicModel"
markers
:min="0"
:max="10"
/>
<q-badge color="secondary">
Model: {{ greenModel }} (0 to 10)
</q-badge>
<q-slider
v-model="greenModel"
color="green"
markers
snap
:min="0"
:max="10"
/>
<q-badge color="secondary">
Model: {{ orangeModel }} (0 to 16, step 2, marker step 4)
</q-badge>
<q-slider
v-model="orangeModel"
color="orange"
snap
:step="2"
:min="0"
:max="16"
:markers="4"
/>
</div>
</template>
Copied to clipboard
Model: 2 (0 to 10)
Model: 0 (0 to 10)
Model: 6 (0 to 16, step 2, marker step 4)
标记标签 v2.4+
标记标签
Template
Script
All
<template>
<div class="q-px-lg q-pt-md q-pb-xl">
<q-slider
v-model="model"
marker-labels
:min="0"
:max="6"
/>
<q-slider
class="q-mt-xl"
v-model="model"
color="deep-orange"
markers
:marker-labels="fnMarkerLabel"
:min="0"
:max="6"
/>
<q-slider
class="q-mt-xl"
v-model="model"
color="purple"
markers
:marker-labels="objMarkerLabel"
:min="0"
:max="6"
/>
<q-slider
class="q-mt-xl"
v-model="priceModel"
color="green"
:inner-min="3"
:inner-max="6"
markers
:marker-labels="arrayMarkerLabel"
label-always
:label-value="priceLabel"
switch-label-side
switch-marker-labels-side
:min="2"
:max="7"
/>
</div>
</template>
Copied to clipboard
0
1
2
3
4
5
6
0%
10%
20%
30%
40%
50%
60%
0°C
3°C
5°C
6°C
$3
$4
$5
$6
$ 4
TIP on slots
为了使用标记标签插槽(参见下面) ,您必须使用 marker-labels
来启用它们。
标记标签插槽
Template
Script
All
<template>
<div class="q-px-lg q-pt-md q-pb-xl">
<q-slider
class="q-mt-xl"
v-model="firstModel"
color="deep-orange"
label-always
markers
marker-labels
:min="1"
:max="10"
:inner-min="2"
:inner-max="8"
>
<template v-slot:marker-label-group="scope">
<div
v-for="marker in scope.markerList"
:key="marker.index"
:class="[ `text-deep-orange-${2 + Math.ceil(marker.value / 2) }`, marker.classes ]"
:style="marker.style"
@click="model = marker.value"
>{{ marker.value }}</div>
</template>
</q-slider>
<q-slider
class="q-mt-xl"
v-model="secondModel"
color="orange"
markers
:min="0"
:max="5"
marker-labels
>
<template v-slot:marker-label-group="{ markerList }">
<div
v-for="val in 4"
:key="val"
class="cursor-pointer"
:class="markerList[val].classes"
:style="markerList[val].style"
@click="secondModel = val"
>{{ val }}</div>
<q-icon
v-for="val in [0, 5]"
:key="val"
:class="markerList[val].classes"
:style="markerList[val].style"
size="sm"
color="orange"
:name="val === 0 ? 'volume_off' : 'volume_up'"
@click="secondModel = val"
/>
</template>
</q-slider>
<q-slider
class="q-mt-xl"
v-model="thirdModel"
color="teal"
:thumb-color="thirdModel === 0 ? 'grey' : 'teal'"
snap
:min="0"
:max="5"
:step="0.5"
marker-labels
switch-marker-labels-side
>
<template v-slot:marker-label-group="{ markerMap }">
<div
class="row items-center no-wrap"
:class="markerMap[thirdModel].classes"
:style="markerMap[thirdModel].style"
>
<q-icon
v-if="thirdModel === 0"
size="xs"
color="teal"
name="star_outline"
/>
<template v-else>
<q-icon
v-for="i in Math.floor(thirdModel)"
:key="i"
size="xs"
color="teal"
name="star_rate"
/>
<q-icon
v-if="thirdModel > Math.floor(thirdModel)"
size="xs"
color="teal"
name="star_half"
/>
</template>
</div>
</template>
</q-slider>
</div>
</template>
Copied to clipboard
2
1
2
3
4
5
6
7
8
9
10
1
2
3
4
volume_offvolume_upstar_ratestar_ratestar_ratestar_half
其他的自定义选项 v2.4+
自定义颜色
Template
Script
All
<template>
<div class="q-pa-lg">
<q-slider
v-model="firstModel"
color="orange"
thumb-color="purple"
label-color="black"
label-text-color="yellow"
markers
marker-labels
marker-labels-class="text-orange"
switch-marker-labels-side
label-always
switch-label-side
:min="0"
:max="6"
/>
<q-slider
class="q-mt-xl"
v-model="secondModel"
color="green"
track-color="orange"
inner-track-color="transparent"
selection-color="red"
:max="10"
markers
/>
<q-slider
v-model="secondModel"
color="purple"
inner-track-color="light-blue-3"
:max="10"
:inner-min="2"
:inner-max="8"
markers
/>
<q-slider
v-model="secondModel"
color="teal"
track-color="light-blue-2"
inner-track-color="light-blue-5"
:max="10"
:inner-min="2"
:inner-max="8"
markers
/>
</div>
</template>
Copied to clipboard
0
1
2
3
4
5
6
2
隐藏选择栏
Template
Script
All
<template>
<div class="q-px-lg q-py-md">
<q-slider
v-model="model"
:min="0"
:max="10"
markers
selection-color="transparent"
/>
<q-slider
v-model="model"
:min="0"
:max="10"
track-color="orange"
inner-track-color="transparent"
selection-color="transparent"
markers
/>
</div>
</template>
Copied to clipboard
自定义轨道图像
Template
Script
All
<template>
<div class="q-pa-lg">
<q-slider
v-model="model"
color="deep-orange"
:max="10"
track-size="8px"
track-color="grey-2"
inner-track-color="transparent"
selection-color="transparent"
:track-img="img"
/>
<q-slider
v-model="model"
color="deep-orange"
:max="10"
track-size="8px"
track-color="white"
inner-track-color="transparent"
:track-img="img"
/>
<q-slider
v-model="model"
color="deep-orange"
:max="10"
track-size="8px"
inner-track-color="transparent"
:selection-img="img"
/>
<q-slider
v-model="model"
color="deep-orange"
:max="10"
:inner-min="2"
:inner-max="8"
track-size="8px"
inner-track-color="white"
:inner-track-img="img"
selection-color="transparent"
/>
</div>
</template>
Copied to clipboard
轨道和滑块的大小
Template
Script
All
<template>
<div class="q-px-lg q-py-md">
<q-slider
v-model="model"
:min="0"
:max="10"
color="green"
track-size="10px"
thumb-color="black"
markers
/>
<q-slider
v-model="model"
:min="0"
:max="10"
color="green"
thumb-size="35px"
markers
/>
</div>
</template>
Copied to clipboard
懒输入
Lazy input
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ lazy }}
</q-badge>
<q-slider
:model-value="lazy"
@change="val => { lazy = val }"
:min="0"
:max="45"
:step="5"
color="purple"
label
/>
</div>
</template>
Copied to clipboard
Model: 6
6
Null 值
Null value
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="primary" class="q-mb-lg">
Model: {{ getNullLabel(basicModel) }} (step 1)
</q-badge>
<q-slider
v-model="basicModel"
/>
<q-badge color="secondary" class="q-mb-lg">
Model: {{ getNullLabel(limitModel) }} (10 to 70, step 1, inner 20 to 55)
</q-badge>
<q-slider
v-model="limitModel"
color="secondary"
:min="10"
:max="70"
:inner-min="20"
:inner-max="55"
/>
</div>
</template>
Copied to clipboard
Model: null (step 1)
Model: null (10 to 70, step 1, inner 20 to 55)
反向
反向
Template
Script
All
<template>
<div class="q-pa-md">
<q-badge color="secondary">
Model: {{ standard }} (0 to 50)
</q-badge>
<q-slider reverse v-model="standard" :min="0" :max="50"/>
<q-slider reverse v-model="standard" :min="0" :max="50" color="green"/>
</div>
</template>
Copied to clipboard
Model: 2 (0 to 50)
黑色, 只读, 禁用
黑色
Template
Script
All
<template>
<div class="q-pa-md bg-grey-10 text-white">
<q-badge color="secondary">
Model: {{ value }} (-20 to 20, step 4)
</q-badge>
<q-slider
v-model="value"
:min="-20"
:max="20"
:step="4"
dark
label
label-always
color="light-green"
/>
<q-badge color="secondary">
Model: {{ value }} (-20 to 20, step 4)
</q-badge>
<q-slider
v-model="value"
:min="-20"
:max="20"
:step="4"
dark
label
label-always
color="red"
/>
</div>
</template>
Copied to clipboard
Model: 0 (-20 to 20, step 4)
0
Model: 0 (-20 to 20, step 4)
0
只读
Template
Script
All
<template>
<div class="q-pa-md">
<q-slider
v-model="basicModel"
readonly
/>
<q-slider
v-model="greenModel"
color="green"
readonly
:min="0"
:max="50"
/>
</div>
</template>
Copied to clipboard
禁用
Template
Script
All
<template>
<div class="q-pa-md">
<q-slider
v-model="basicModel"
disable
/>
<q-slider
v-model="greenModel"
color="green"
disable
:min="0"
:max="50"
/>
</div>
</template>
Copied to clipboard
搭配 QItem
搭配 QItem
Template
Script
All
<template>
<div class="q-pa-md">
<q-list dense>
<q-item>
<q-item-section avatar>
<q-icon color="teal" name="volume_up" />
</q-item-section>
<q-item-section>
<q-slider
v-model="volume"
:min="0"
:max="10"
label
color="teal"
/>
</q-item-section>
</q-item>
<q-item>
<q-item-section avatar>
<q-icon color="deep-orange" name="brightness_medium" />
</q-item-section>
<q-item-section>
<q-slider
v-model="brightness"
:min="0"
:max="10"
label
color="deep-orange"
/>
</q-item-section>
</q-item>
<q-item>
<q-item-section avatar>
<q-icon color="primary" name="mic" />
</q-item-section>
<q-item-section>
<q-slider
v-model="mic"
:min="0"
:max="50"
label
/>
</q-item-section>
</q-item>
<q-separator inset spaced />
<q-item>
<q-item-section side>
<q-icon name="volume_down" />
</q-item-section>
<q-item-section>
<q-slider
v-model="volume"
:min="0"
:max="10"
label
/>
</q-item-section>
<q-item-section side>
<q-icon name="volume_up" />
</q-item-section>
</q-item>
</q-list>
</div>
</template>
Copied to clipboard
volume_up
6
brightness_medium
3
mic
8
volume_down
6
volume_up
原生表单提交
当处理一个带有 action
和 method
的原生表单时(如:使用 Quasar 和 ASP.NET 控制器时),您需要为 QSlider 声明 name
属性,否则表单数据中不会包含它:
原生表单
Template
Script
All
<template>
<div class="q-pa-md">
<q-form @submit="onSubmit" class="q-gutter-md">
<div class="q-mt-xl">
<q-slider
name="speed"
v-model="speed"
label-always
:min="20"
:max="140"
:step="10"
/>
</div>
<div>
<q-btn label="Submit" type="submit" color="primary"/>
</div>
</q-form>
<q-card v-if="submitResult.length > 0" flat bordered class="q-mt-md bg-grey-2">
<q-card-section>Submitted form contains the following formData (key = value):</q-card-section>
<q-separator />
<q-card-section class="row q-gutter-sm items-center">
<div
v-for="(item, index) in submitResult"
:key="index"
class="q-px-sm q-py-xs bg-grey-8 text-white rounded-borders text-center text-no-wrap"
>{{ item.name }} = {{ item.value }}</div>
</q-card-section>
</q-card>
</div>
</template>
Copied to clipboard
类型定义
interface SliderMarkerLabelPartialDefinition {
label?: number | string;
classes?: VueClassProp;
style?: VueStyleObjectProp;
}
interface SliderMarkerLabelDefinition
extends SliderMarkerLabelPartialDefinition {
value?: number;
}
interface SliderMarkerLabelDefinitionRequiredValue
extends SliderMarkerLabelPartialDefinition {
value: number;
}
interface SliderMarkerLabelObjectDefinition {
[value: number]: string | SliderMarkerLabelDefinition;
}
export type SliderMarkerLabels =
| boolean
| Array<SliderMarkerLabelDefinitionRequiredValue>
| SliderMarkerLabelObjectDefinition
| ((value: number) => string | SliderMarkerLabelDefinition);
export type SliderMarkerLabelConfig = {
index: number;
value: number;
label: number | string;
classes: string;
style: VueStyleObjectProp;
};
export type SliderMarkerLabelArrayConfig = SliderMarkerLabelConfig[];
export interface SliderMarkerLabelObjectConfig {
[value: number]: SliderMarkerLabelConfig;
}
版权声明 本中文文档内容版权为站长个人所有,保留所有权利。
免责声明 本站点所包含的任何跳转链接均为第三方广告内容,站点对这些广告内容的准确性、合法性、真实性不承担任何责任。用户点击跳转链接进入第三方网站后,应自行判断广告内容的真实性和合法性,并自行承担因使用广告内容而产生的风险和责任。任何因使用第三方广告内容而导致的损失或损害,本站点概不负责。