滑块
v-slider
组件是数字输入的更好的可视化,它用于收集数字用户数据。
用例
滑块沿着条形图反映一系列值,用户可以从中选择单个值。 它们是调整设置的理想选择,例如音量,亮度或应用图像滤镜。
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<v-row
justify="space-around"
>
<v-col cols="12">
<v-slider
v-model="min"
min="-100"
max="100"
label="Min"
></v-slider>
</v-col>
<v-col cols="12">
<v-slider
v-model="max"
min="-100"
max="100"
label="Max"
></v-slider>
</v-col>
<v-switch v-model="disabled" class="ma-2" label="Disabled"></v-switch>
<v-switch v-model="readonly" class="ma-2" label="Readonly"></v-switch>
<v-switch v-model="vertical" class="ma-2" label="Vertical"></v-switch>
<v-switch v-model="range" class="ma-2" label="Range"></v-switch>
<v-col cols="12">
<v-slider
v-if="!range"
v-model="volume"
append-icon="volume_up"
prepend-icon="volume_down"
:min="min"
:max="max"
:disabled="disabled"
:readonly="readonly"
:vertical="vertical"
label="Volume"
></v-slider>
<v-range-slider
v-else
append-icon="volume_up"
prepend-icon="volume_down"
:min="min"
:max="max"
:disabled="disabled"
:readonly="readonly"
:vertical="vertical"
label="Volume"
></v-range-slider>
</v-col>
</v-row>
</template>
<script>
export default {
data () {
return {
volume: 10,
min: 0,
max: 100,
disabled: false,
readonly: false,
vertical: false,
range: false,
}
},
}
</script>
示例
下面是一些简单到复杂的例子。
最小值和最大值
您可以设置滑块的 min
和 max
值。
template script
<template>
<v-card flat color="transparent">
<v-subheader>Min and max default slider</v-subheader>
<v-card-text>
<v-row>
<v-col class="pr-4">
<v-slider
v-model="slider"
class="align-center"
:max="max"
:min="min"
hide-details
>
<template v-slot:append>
<v-text-field
v-model="slider"
class="mt-0 pt-0"
hide-details
single-line
type="number"
style="width: 60px"
></v-text-field>
</template>
</v-slider>
</v-col>
</v-row>
</v-card-text>
<v-subheader>Min and max range slider</v-subheader>
<v-card-text>
<v-row>
<v-col class="px-4">
<v-range-slider
v-model="range"
:max="max"
:min="min"
hide-details
class="align-center"
>
<template v-slot:prepend>
<v-text-field
:value="range[0]"
class="mt-0 pt-0"
hide-details
single-line
type="number"
style="width: 60px"
@change="$set(range, 0, $event)"
></v-text-field>
</template>
<template v-slot:append>
<v-text-field
:value="range[1]"
class="mt-0 pt-0"
hide-details
single-line
type="number"
style="width: 60px"
@change="$set(range, 1, $event)"
></v-text-field>
</template>
</v-range-slider>
</v-col>
</v-row>
</v-card-text>
</v-card>
</template>
<script>
export default {
data () {
return {
min: -50,
max: 90,
slider: 40,
range: [-20, 70],
}
},
}
</script>
禁用
您无法与 disabled
滑块互动。
template
<template>
<v-container fluid>
<v-row>
<v-col cols="12">
<v-slider
disabled
label="Disabled"
value="30"
></v-slider>
</v-col>
</v-row>
</v-container>
</template>
只读
您不能与 readonly
滑块交互,但它们看起来与普通滑块一样。
template
<template>
<v-container fluid>
<v-row>
<v-col cols="12">
<v-slider
readonly
label="Readonly"
value="30"
></v-slider>
</v-col>
</v-row>
</v-container>
</template>
图标
您可以使用 append-icon
和 prepend-icon
属性将图标添加到滑块。
template script
<template>
<v-card
flat
color="transparent"
>
<v-subheader>Media volume</v-subheader>
<v-card-text>
<v-slider
v-model="media"
prepend-icon="volume_up"
></v-slider>
</v-card-text>
<v-subheader>Alarm volume</v-subheader>
<v-card-text>
<v-slider
v-model="alarm"
append-icon="alarm"
></v-slider>
</v-card-text>
<v-subheader>Icon click callback</v-subheader>
<v-card-text>
<v-slider
v-model="zoom"
append-icon="zoom_in"
prepend-icon="zoom_out"
@click:append="zoomIn"
@click:prepend="zoomOut"
></v-slider>
</v-card-text>
</v-card>
</template>
<script>
export default {
data () {
return {
media: 0,
alarm: 0,
zoom: 0,
}
},
methods: {
zoomOut () {
this.zoom = (this.zoom - 10) || 0
},
zoomIn () {
this.zoom = (this.zoom + 10) || 100
},
},
}
</script>
垂直滑块
您可以使用 vertical
将滑块切换到垂直方向。 如果需要更改滑块的高度,请使用 css。
template script
<template>
<v-container>
<v-row>
<v-slider
v-model="value"
vertical
label="Regular"
></v-slider>
<v-range-slider
v-model="value2"
vertical
label="Range"
></v-range-slider>
</v-row>
</v-container>
</template>
<script>
export default {
data () {
return {
value: 10,
value2: [20, 40],
}
},
}
</script>
拇指
您可以在滑动或总是显示“缩略图标签”。它可以通过设置“缩略图-颜色”和自定义大小,设置“缩略图-大小”来显示自定义颜色。 使用 always-dirty
时,即使在 min
值上,其颜色永远不会改变。
template script
<template>
<v-container fluid>
<v-row>
<v-col cols="12">
<v-subheader class="pl-0">Show thumb when using slider</v-subheader>
<v-slider
v-model="slider"
thumb-label
></v-slider>
</v-col>
<v-col cols="12">
<v-subheader class="pl-0">Always show thumb label</v-subheader>
<v-slider
v-model="slider"
thumb-label="always"
></v-slider>
</v-col>
<v-col cols="12">
<v-subheader class="pl-0">Custom thumb size</v-subheader>
<v-slider
v-model="slider"
:thumb-size="24"
thumb-label="always"
></v-slider>
</v-col>
<v-col cols="12">
<v-subheader class="pl-0">Custom thumb label</v-subheader>
<v-slider
v-model="slider"
:thumb-size="24"
thumb-label="always"
>
<template v-slot:thumb-label="{ value }">
{{ satisfactionEmojis[Math.min(Math.floor(value / 10), 9)] }}
</template>
</v-slider>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
data () {
return {
satisfactionEmojis: ['😭', '😢', '☹️', '🙁', '😐', '🙂', '😊', '😁', '😄', '😍'],
slider: 45,
}
},
}
</script>
反向标签
具有 inverse-label
属性的 v-slider
在其末尾显示标签。
template
<template>
<v-container fluid>
<v-row>
<v-col cols="12">
<v-slider
inverse-label
label="Inverse label"
value="30"
></v-slider>
</v-col>
</v-row>
</v-container>
</template>
Light Vuetify Stickers
Stick them to your car, laptop or favorite mug. They are made from a durable vinyl with a laminate that is weather-proof against rain, sun, wind and snow, and even dishwasher safe.
ads by Vuetify
](https://store.vuetifyjs.com/product/vuetify-light-sticker?ref=vuetifyjs.com)
自定义范围滑块
使用 tick-labels
属性和插槽,您可以创建一个非常定制的解决方案。
template script
<template>
<v-row>
<v-col class="pa-12">
<v-range-slider
:tick-labels="seasons"
:value="[0, 1]"
min="0"
max="3"
ticks="always"
tick-size="4"
>
<template v-slot:thumb-label="props">
<v-icon dark>
{{ season(props.value) }}
</v-icon>
</template>
</v-range-slider>
</v-col>
</v-row>
</template>
<script>
export default {
data: () => ({
seasons: [
'Winter',
'Spring',
'Summer',
'Fall',
],
icons: [
'mdi-snowflake',
'mdi-leaf',
'mdi-fire',
'mdi-water',
],
}),
methods: {
season (val) {
return this.icons[val]
},
},
}
</script>
刻度线
刻度线代表用户可以将滑块移动到的预定值。
template script
<template>
<v-card flat color="transparent">
<v-subheader>Show ticks when using slider</v-subheader>
<v-card-text>
<v-slider
v-model="value"
step="10"
ticks
></v-slider>
</v-card-text>
<v-subheader>Always show ticks</v-subheader>
<v-card-text>
<v-slider
v-model="value"
step="10"
ticks="always"
></v-slider>
</v-card-text>
<v-subheader>Tick size</v-subheader>
<v-card-text>
<v-slider
v-model="value"
step="10"
ticks="always"
tick-size="4"
></v-slider>
</v-card-text>
<v-subheader>Tick labels</v-subheader>
<v-card-text>
<v-slider
v-model="fruits"
:tick-labels="ticksLabels"
:max="3"
step="1"
ticks="always"
tick-size="4"
></v-slider>
</v-card-text>
</v-card>
</template>
<script>
export default {
data () {
return {
value: 0,
fruits: 0,
ticksLabels: [
'Figs',
'Lemon',
'Pear',
'Apple',
],
}
},
}
</script>
自定义颜色
您可以使用color
、track-color
和thumb-color
属性来设置滑块的颜色。
template script
<template>
<div>
<v-slider
v-model="ex1.val"
:color="ex1.color"
:label="ex1.label"
></v-slider>
<v-slider
v-model="ex2.val"
:label="ex2.label"
:track-color="ex2.color"
></v-slider>
<v-slider
v-model="ex3.val"
:label="ex3.label"
:thumb-color="ex3.color"
thumb-label="always"
></v-slider>
</div>
</template>
<script>
export default {
data () {
return {
ex1: { label: 'color', val: 25, color: 'orange darken-3' },
ex2: { label: 'track-color', val: 75, color: 'green lighten-1' },
ex3: { label: 'thumb-color', val: 50, color: 'red' },
}
},
}
</script>
范围
范围滑块。
template script
<template>
<v-card flat color="transparent">
<v-subheader>Default range slider</v-subheader>
<v-card-text>
<v-range-slider
v-model="value1"
></v-range-slider>
</v-card-text>
<v-subheader>Disabled range slider</v-subheader>
<v-card-text>
<v-range-slider
v-model="value2"
disabled
></v-range-slider>
</v-card-text>
</v-card>
</template>
<script>
export default {
data () {
return {
value1: [30, 60],
value2: [30, 60],
}
},
}
</script>
验证
Vuetify包含简单的验证通过使用 rules
属性,这个属性接受一个回调函数组,在验证规则时,当前的 v-model 值将被传递给回调函数,这个回调函数必须返回 ture
或 String
或者错误信息。
template script
<template>
<v-card flat color="transparent">
<v-subheader>Rules</v-subheader>
<v-card-text class="pt-0">
<v-slider
v-model="value"
:rules="rules"
label="How many?"
step="10"
thumb-label="always"
ticks
></v-slider>
</v-card-text>
<v-subheader>Persistent hint</v-subheader>
<v-card-text class="pt-0">
<v-slider
v-model="value"
:rules="rules"
hint="40 in stock"
label="How many?"
persistent-hint
step="10"
thumb-label="always"
ticks
></v-slider>
</v-card-text>
</v-card>
</template>
<script>
export default {
data () {
return {
value: 30,
rules: [
v => v <= 40 || 'Only 40 in stock',
],
}
},
}
</script>
插槽
使用诸如 prepend
和 append
之类的插槽可轻松自定义 v-slider
以适应任何情况。
template script style
<template>
<v-card
class="mx-auto"
max-width="600"
>
<v-toolbar
flat
dense
>
<v-toolbar-title>
<span class="subheading">METRONOME</span>
</v-toolbar-title>
<v-spacer></v-spacer>
<v-btn icon>
<v-icon>mdi-share-variant</v-icon>
</v-btn>
</v-toolbar>
<v-card-text>
<v-row
class="mb-4"
justify="space-between"
>
<v-col class="text-left">
<span
class="display-3 font-weight-light"
v-text="bpm"
></span>
<span class="subheading font-weight-light mr-1">BPM</span>
<v-fade-transition>
<v-avatar
v-if="isPlaying"
:color="color"
:style="{
animationDuration: animationDuration
}"
class="mb-1 v-avatar--metronome"
size="12"
></v-avatar>
</v-fade-transition>
</v-col>
<v-col class="text-right">
<v-btn
:color="color"
dark
depressed
fab
@click="toggle"
>
<v-icon large>
{{ isPlaying ? 'mdi-pause' : 'mdi-play' }}
</v-icon>
</v-btn>
</v-col>
</v-row>
<v-slider
v-model="bpm"
:color="color"
track-color="grey"
always-dirty
min="40"
max="218"
>
<template v-slot:prepend>
<v-icon
:color="color"
@click="decrement"
>
mdi-minus
</v-icon>
</template>
<template v-slot:append>
<v-icon
:color="color"
@click="increment"
>
mdi-plus
</v-icon>
</template>
</v-slider>
</v-card-text>
</v-card>
</template>
<script>
export default {
data: () => ({
bpm: 40,
interval: null,
isPlaying: false,
}),
computed: {
color () {
if (this.bpm < 100) return 'indigo'
if (this.bpm < 125) return 'teal'
if (this.bpm < 140) return 'green'
if (this.bpm < 175) return 'orange'
return 'red'
},
animationDuration () {
return `${60 / this.bpm}s`
},
},
methods: {
decrement () {
this.bpm--
},
increment () {
this.bpm++
},
toggle () {
this.isPlaying = !this.isPlaying
},
},
}
</script>
<style>
@keyframes metronome-example {
from {
transform: scale(.5);
}
to {
transform: scale(1);
}
}
.v-avatar--metronome {
animation-name: metronome-example;
animation-iteration-count: infinite;
animation-direction: alternate;
}
</style>