Dropdown 下拉菜单
将动作或菜单折叠到下拉菜单中。
基础用法
移动到下拉菜单上,展开更多操作。
通过组件slot
来设置下拉触发的元素以及需要通过具名slot
为dropdown
来设置下拉菜单。默认情况下,下拉按钮只要hover
即可,无需点击也会显示下拉菜单。
1 <el-dropdown> 2 <span class="el-dropdown-link"> 3 下拉菜单<i class="el-icon-arrow-down el-icon--right"></i> 4 </span> 5 <el-dropdown-menu slot="dropdown"> 6 <el-dropdown-item>黄金糕</el-dropdown-item> 7 <el-dropdown-item>狮子头</el-dropdown-item> 8 <el-dropdown-item>螺蛳粉</el-dropdown-item> 9 <el-dropdown-item disabled>双皮奶</el-dropdown-item> 10 <el-dropdown-item divided>蚵仔煎</el-dropdown-item> 11 </el-dropdown-menu> 12 </el-dropdown>
触发对象
可使用按钮触发下拉菜单。
设置split-button
属性来让触发下拉元素呈现为按钮组,左边是功能按钮,右边是触发下拉菜单的按钮,设置为true
即可。
1 <el-dropdown> 2 <el-button type="primary"> 3 更多菜单<i class="el-icon-arrow-down el-icon--right"></i> 4 </el-button> 5 <el-dropdown-menu slot="dropdown"> 6 <el-dropdown-item>黄金糕</el-dropdown-item> 7 <el-dropdown-item>狮子头</el-dropdown-item> 8 <el-dropdown-item>螺蛳粉</el-dropdown-item> 9 <el-dropdown-item>双皮奶</el-dropdown-item> 10 <el-dropdown-item>蚵仔煎</el-dropdown-item> 11 </el-dropdown-menu> 12 </el-dropdown> 13 <el-dropdown split-button type="primary" @click="handleClick"> 14 更多菜单 15 <el-dropdown-menu slot="dropdown"> 16 <el-dropdown-item>黄金糕</el-dropdown-item> 17 <el-dropdown-item>狮子头</el-dropdown-item> 18 <el-dropdown-item>螺蛳粉</el-dropdown-item> 19 <el-dropdown-item>双皮奶</el-dropdown-item> 20 <el-dropdown-item>蚵仔煎</el-dropdown-item> 21 </el-dropdown-menu> 22 </el-dropdown> 23 24 <style> 25 .el-dropdown { 26 vertical-align: top; 27 } 28 .el-dropdown + .el-dropdown { 29 margin-left: 15px; 30 } 31 .el-icon-arrow-down { 32 font-size: 12px; 33 } 34 </style> 35 36 <script> 37 export default { 38 methods: { 39 handleClick() { 40 alert('button click'); 41 } 42 } 43 } 44 </script>
触发方式
可以配置 click 激活或者 hover 激活。
在trigger
属性设置为click
即可。
1 <el-row class="block-col-2"> 2 <el-col :span="12"> 3 <span class="demonstration">hover 激活</span> 4 <el-dropdown> 5 <span class="el-dropdown-link"> 6 下拉菜单<i class="el-icon-arrow-down el-icon--right"></i> 7 </span> 8 <el-dropdown-menu slot="dropdown"> 9 <el-dropdown-item>黄金糕</el-dropdown-item> 10 <el-dropdown-item>狮子头</el-dropdown-item> 11 <el-dropdown-item>螺蛳粉</el-dropdown-item> 12 <el-dropdown-item>双皮奶</el-dropdown-item> 13 <el-dropdown-item>蚵仔煎</el-dropdown-item> 14 </el-dropdown-menu> 15 </el-dropdown> 16 </el-col> 17 <el-col :span="12"> 18 <span class="demonstration">click 激活</span> 19 <el-dropdown trigger="click"> 20 <span class="el-dropdown-link"> 21 下拉菜单<i class="el-icon-arrow-down el-icon--right"></i> 22 </span> 23 <el-dropdown-menu slot="dropdown"> 24 <el-dropdown-item>黄金糕</el-dropdown-item> 25 <el-dropdown-item>狮子头</el-dropdown-item> 26 <el-dropdown-item>螺蛳粉</el-dropdown-item> 27 <el-dropdown-item>双皮奶</el-dropdown-item> 28 <el-dropdown-item>蚵仔煎</el-dropdown-item> 29 </el-dropdown-menu> 30 </el-dropdown> 31 </el-col> 32 </el-row>
菜单隐藏方式
可以hide-on-click
属性来配置。
下拉菜单默认在点击菜单项后会被隐藏,将hide-on-click
属性默认为false
可以关闭此功能。
1 <el-dropdown :hide-on-click="false"> 2 <span class="el-dropdown-link"> 3 下拉菜单<i class="el-icon-arrow-down el-icon--right"></i> 4 </span> 5 <el-dropdown-menu slot="dropdown"> 6 <el-dropdown-item>黄金糕</el-dropdown-item> 7 <el-dropdown-item>狮子头</el-dropdown-item> 8 <el-dropdown-item>螺蛳粉</el-dropdown-item> 9 <el-dropdown-item disabled>双皮奶</el-dropdown-item> 10 <el-dropdown-item divided>蚵仔煎</el-dropdown-item> 11 </el-dropdown-menu> 12 </el-dropdown> 13 14 <style> 15 .el-dropdown-link { 16 cursor: pointer; 17 color: #409EFF; 18 } 19 .el-icon-arrow-down { 20 font-size: 12px; 21 } 22 </style>
指令事件
点击菜单项后会触发事件,用户可以通过相应的菜单项 key 进行不同的操作
1 <el-dropdown @command="handleCommand"> 2 <span class="el-dropdown-link"> 3 下拉菜单<i class="el-icon-arrow-down el-icon--right"></i> 4 </span> 5 <el-dropdown-menu slot="dropdown"> 6 <el-dropdown-item command="a">黄金糕</el-dropdown-item> 7 <el-dropdown-item command="b">狮子头</el-dropdown-item> 8 <el-dropdown-item command="c">螺蛳粉</el-dropdown-item> 9 <el-dropdown-item command="d" disabled>双皮奶</el-dropdown-item> 10 <el-dropdown-item command="e" divided>蚵仔煎</el-dropdown-item> 11 </el-dropdown-menu> 12 </el-dropdown> 13 14 <style> 15 .el-dropdown-link { 16 cursor: pointer; 17 color: #409EFF; 18 } 19 .el-icon-arrow-down { 20 font-size: 12px; 21 } 22 </style> 23 24 <script> 25 export default { 26 methods: { 27 handleCommand(command) { 28 this.$message('click on item ' + command); 29 } 30 } 31 } 32 </script>
不同尺寸
Dropdown 组件提供除了默认值以外的三种尺寸,可以在不同场景下选择合适的尺寸。
额外的尺寸:medium
、small
、mini
,通过设置size
属性来配置它们。
1 <el-dropdown split-button type="primary"> 2 默认尺寸 3 <el-dropdown-menu slot="dropdown"> 4 <el-dropdown-item>黄金糕</el-dropdown-item> 5 <el-dropdown-item>狮子头</el-dropdown-item> 6 <el-dropdown-item>螺蛳粉</el-dropdown-item> 7 <el-dropdown-item>双皮奶</el-dropdown-item> 8 <el-dropdown-item>蚵仔煎</el-dropdown-item> 9 </el-dropdown-menu> 10 </el-dropdown> 11 12 <el-dropdown size="medium" split-button type="primary"> 13 中等尺寸 14 <el-dropdown-menu slot="dropdown"> 15 <el-dropdown-item>黄金糕</el-dropdown-item> 16 <el-dropdown-item>狮子头</el-dropdown-item> 17 <el-dropdown-item>螺蛳粉</el-dropdown-item> 18 <el-dropdown-item>双皮奶</el-dropdown-item> 19 <el-dropdown-item>蚵仔煎</el-dropdown-item> 20 </el-dropdown-menu> 21 </el-dropdown> 22 23 <el-dropdown size="small" split-button type="primary"> 24 小型尺寸 25 <el-dropdown-menu slot="dropdown"> 26 <el-dropdown-item>黄金糕</el-dropdown-item> 27 <el-dropdown-item>狮子头</el-dropdown-item> 28 <el-dropdown-item>螺蛳粉</el-dropdown-item> 29 <el-dropdown-item>双皮奶</el-dropdown-item> 30 <el-dropdown-item>蚵仔煎</el-dropdown-item> 31 </el-dropdown-menu> 32 </el-dropdown> 33 34 <el-dropdown size="mini" split-button type="primary"> 35 超小尺寸 36 <el-dropdown-menu slot="dropdown"> 37 <el-dropdown-item>黄金糕</el-dropdown-item> 38 <el-dropdown-item>狮子头</el-dropdown-item> 39 <el-dropdown-item>螺蛳粉</el-dropdown-item> 40 <el-dropdown-item>双皮奶</el-dropdown-item> 41 <el-dropdown-item>蚵仔煎</el-dropdown-item> 42 </el-dropdown-menu> 43 </el-dropdown>
Dropdown Attributes
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
type | 菜单按钮类型,同 Button 组件(只在split-button 为 true 的情况下有效) |
string | — | — |
size | 菜单尺寸,在split-button 为 true 的情况下也对触发按钮生效 |
string | medium / small / mini | — |
split-button | 下拉触发元素呈现为按钮组 | boolean | — | false |
placement | 菜单弹出位置 | string | top/top-start/top-end/bottom/bottom-start/bottom-end | bottom-end |
trigger | 触发下拉的行为 | string | hover, click | hover |
hide-on-click | 是否在点击菜单项后隐藏菜单 | boolean | — | true |
show-timeout | 展开下拉菜单的延时(仅在 trigger 为 hover 时有效) | number | — | 250 |
hide-timeout | 收起下拉菜单的延时(仅在 trigger 为 hover 时有效) | number | — | 150 |
Dropdown Events
事件名称 | 说明 | 回调参数 |
---|---|---|
click | split-button 为 true 时,点击左侧按钮的回调 |
— |
command | 点击菜单项触发的事件回调 | dropdown-item 的指令 |
visible-change | 下拉框出现/隐藏时触发 | 出现则为 true,隐藏则为 false |
Dropdown Menu Item Attributes
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
command | 指令 | string/number/object | — | — |
disabled | 禁用 | boolean | — | false |
divided | 显示分割线 | boolean | — | false |