菜单
v-menu
组件在激活它的元素的位置上展示一个菜单。
用例
请记住将激活菜单的元素放置在 activator
插槽中。
template script
<template>
<div class="text-center">
<v-menu offset-y>
<template v-slot:activator="{ on }">
<v-btn
color="primary"
dark
v-on="on"
>
Dropdown
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
}),
}
</script>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<v-row align="center">
<v-row justify="space-around">
<v-switch v-model="disabled" class="ma-2" label="Disabled"></v-switch>
<v-switch v-model="absolute" class="ma-2" label="Absolute"></v-switch>
<v-switch v-model="openOnHover" class="ma-2" label="Open on hover"></v-switch>
<v-switch v-model="closeOnClick" label="Close on click"></v-switch>
<v-switch v-model="closeOnContentClick" label="Close on content click"></v-switch>
<v-switch v-model="offsetX" label="X offset"></v-switch>
<v-switch v-model="offsetY" label="Y offset"></v-switch>
<v-switch v-model="value" class="ma-2" label="Value"></v-switch>
</v-row>
<v-menu
v-model="value"
:disabled="disabled"
:absolute="absolute"
:open-on-hover="openOnHover"
:close-on-click="closeOnClick"
:close-on-content-click="closeOnContentClick"
:offset-x="offsetX"
:offset-y="offsetY"
>
<template v-slot:activator="{ on }">
<v-btn
color="primary"
dark
v-on="on"
>
Dropdown
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-row>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
disabled: false,
absolute: false,
openOnHover: false,
value: false,
closeOnClick: true,
closeOnContentClick: true,
offsetX: false,
offsetY: true,
}),
}
</script>
示例
下面是一些简单到复杂的例子。
绝对定位
菜单也可以使用absolute
属性绝对定位在激活元素的顶部。尝试点击图片上的任何地方。
template script
<template>
<v-row
class="d-flex"
justify="center"
>
<v-menu
v-model="showMenu"
absolute
offset-y
style="max-width: 600px"
>
<template v-slot:activator="{ on }">
<v-card
class="portrait"
img="https://cdn.vuetifyjs.com/images/cards/girl.jpg"
height="300"
width="600"
v-on="on"
></v-card>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-row>
</template>
<script>
export default {
data: () => ({
showMenu: false,
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
}),
}
</script>
菜单带有激活器和提示
在新的 v-slot
语法中,嵌套的激活器(比如与同一个激活器按钮相连的 v-menu
和 v-tooltip
)需要特殊的设置才能正常工作。注意:同样的语法也适用于其他嵌套的激活器,比如 v-dialog
w/ v-tooltip
。
template script
<template>
<div class="text-center">
<v-menu>
<template v-slot:activator="{ on: menu }">
<v-tooltip bottom>
<template v-slot:activator="{ on: tooltip }">
<v-btn
color="primary"
dark
v-on="{ ...tooltip, ...menu }"
>Dropdown w/ Tooltip</v-btn>
</template>
<span>Im A ToolTip</span>
</v-tooltip>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me1' },
{ title: 'Click Me2' },
{ title: 'Click Me3' },
{ title: 'Click Me4' },
],
}),
}
</script>
悬停
菜单可以使用悬停进行访问,而不是点击 open-on-hover
属性。
template script
<template>
<div class="text-center">
<v-menu open-on-hover top offset-y>
<template v-slot:activator="{ on }">
<v-btn
color="primary"
dark
v-on="on"
>
Dropdown
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
}),
}
</script>
自定义过渡
Vuetify 带有3个标准过渡,scale、slide-x 和 slide-y。您也可以创建自己的并将其作为过渡参数传递。有关如何构建内置过渡的示例,请访问 here。
template script
<template>
<v-row justify="space-around">
<v-menu
bottom
origin="center center"
transition="scale-transition"
>
<template v-slot:activator="{ on }">
<v-btn
color="primary"
dark
v-on="on"
>
Scale Transition
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, i) in items"
:key="i"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
<v-menu
transition="slide-x-transition"
bottom
right
>
<template v-slot:activator="{ on }">
<v-btn
class="deep-orange"
color="primary"
dark
v-on="on"
>
Slide X Transition
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, i) in items"
:key="i"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
<v-menu
transition="slide-y-transition"
bottom
>
<template v-slot:activator="{ on }">
<v-btn
class="purple"
color="primary"
dark
v-on="on"
>
Slide Y Transition
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, i) in items"
:key="i"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-row>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
}),
}
</script>
禁用
您可以禁用菜单。 禁用的菜单无法打开。
template script
<template>
<div class="text-center">
<v-menu disabled top offset-y>
<template v-slot:activator="{ on }">
<v-btn
color="primary"
dark
v-on="on"
>
Dropdown
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
}),
}
</script>
X 偏移
菜单可以通过 X 轴偏移,以使激活器可见。
template script
<template>
<div class="text-center">
<v-switch v-model="offset" label="X offset"></v-switch>
<v-menu top :offset-x="offset">
<template v-slot:activator="{ on }">
<v-btn
color="primary"
dark
v-on="on"
>
Dropdown
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
offset: true,
}),
}
</script>
Status Page
You can check the status of the documentation any time on our OhDear! uptime page.
ads by Vuetify
](https://status.vuetifyjs.com?ref=vuetifyjs.com)
Y 偏移
菜单可以通过 Y 轴偏移,以使激活器可见。
template script
<template>
<div class="text-center">
<v-switch v-model="offset" label="Y offset"></v-switch>
<v-menu top :offset-y="offset">
<template v-slot:activator="{ on }">
<v-btn
color="primary"
dark
v-on="on"
>
Dropdown
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
offset: true,
}),
}
</script>
点击关闭
失去焦点时可以关闭菜单。
template script
<template>
<div class="text-center">
<v-switch v-model="closeOnClick" label="Close on click"></v-switch>
<v-menu top :close-on-click="closeOnClick">
<template v-slot:activator="{ on }">
<v-btn
color="primary"
dark
v-on="on"
>
Dropdown
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
closeOnClick: true,
}),
}
</script>
点击内容后关闭
您可以配置单击 v-menu
的内容时是否应关闭它。
template script
<template>
<div class="text-center">
<v-switch v-model="closeOnContentClick" label="Close on content click"></v-switch>
<v-menu top :close-on-content-click="closeOnContentClick">
<template v-slot:activator="{ on }">
<v-btn
color="primary"
dark
v-on="on"
>
Dropdown
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
closeOnContentClick: true,
}),
}
</script>
没有激活时的绝对定位
菜单也可以通过 absolute
属性跟 position-x
和 position-y
属性一起使用。尝试右键单击图像上的任意位置。
template script style
<template>
<div>
<v-row class="flex" justify="center">
<v-card
:ripple="false"
class="portrait"
img="https://cdn.vuetifyjs.com/images/cards/girl.jpg"
height="300px"
@click="show"
></v-card>
</v-row>
<v-menu
v-model="showMenu"
:position-x="x"
:position-y="y"
absolute
offset-y
>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
showMenu: false,
x: 0,
y: 0,
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
}),
methods: {
show (e) {
e.preventDefault()
this.showMenu = false
this.x = e.clientX
this.y = e.clientY
this.$nextTick(() => {
this.showMenu = true
})
},
},
}
</script>
<style scoped>
.portrait.v-card {
margin: 0 auto;
max-width: 600px;
width: 100%;
}
</style>
菜单
菜单可以放在几乎任何组件中
template script
<template>
<v-row>
<v-col cols="12" sm="6" offset-sm="3">
<v-card height="200px">
<v-card-title class="blue white--text">
<span class="headline">Menu</span>
<v-spacer></v-spacer>
<v-menu bottom left>
<template v-slot:activator="{ on }">
<v-btn
dark
icon
v-on="on"
>
<v-icon>mdi-dots-vertical</v-icon>
</v-btn>
</template>
<v-list>
<v-list-item
v-for="(item, i) in items"
:key="i"
@click=""
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-card-title>
<v-card-text>Lorem Ipsum</v-card-text>
</v-card>
</v-col>
</v-row>
</template>
<script>
export default {
data: () => ({
items: [
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me' },
{ title: 'Click Me 2' },
],
}),
}
</script>
弹出菜单
菜单可以配置为在打开时为静态菜单,使其充当弹出菜单。 当菜单内容中有多个交互式项目时,这很有用。
template script
<template>
<div class="text-center">
<v-menu
v-model="menu"
:close-on-content-click="false"
:nudge-width="200"
offset-x
>
<template v-slot:activator="{ on }">
<v-btn
color="indigo"
dark
v-on="on"
>
Menu as Popover
</v-btn>
</template>
<v-card>
<v-list>
<v-list-item>
<v-list-item-avatar>
<img src="https://cdn.vuetifyjs.com/images/john.jpg" alt="John">
</v-list-item-avatar>
<v-list-item-content>
<v-list-item-title>John Leider</v-list-item-title>
<v-list-item-subtitle>Founder of Vuetify.js</v-list-item-subtitle>
</v-list-item-content>
<v-list-item-action>
<v-btn
:class="fav ? 'red--text' : ''"
icon
@click="fav = !fav"
>
<v-icon>mdi-heart</v-icon>
</v-btn>
</v-list-item-action>
</v-list-item>
</v-list>
<v-divider></v-divider>
<v-list>
<v-list-item>
<v-list-item-action>
<v-switch v-model="message" color="purple"></v-switch>
</v-list-item-action>
<v-list-item-title>Enable messages</v-list-item-title>
</v-list-item>
<v-list-item>
<v-list-item-action>
<v-switch v-model="hints" color="purple"></v-switch>
</v-list-item-action>
<v-list-item-title>Enable hints</v-list-item-title>
</v-list-item>
</v-list>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn text @click="menu = false">Cancel</v-btn>
<v-btn color="primary" text @click="menu = false">Save</v-btn>
</v-card-actions>
</v-card>
</v-menu>
</div>
</template>
<script>
export default {
data: () => ({
fav: true,
menu: false,
message: false,
hints: true,
}),
}
</script>
无障碍
默认情况下,v-menu
组件被 detached 并移动到你的应用程序的根目录下。为了正确地支持 将动态内容插入到 DOM,你必须_使用 attach prop。这将确保在按 tab 键时,焦点从激活器转移到内容上。