Dropdown 下拉菜单

将动作或菜单折叠到下拉菜单中。

基础用法

移动到下拉菜单上,展开更多操作。

Dropdown 下拉菜单 - 图1

通过组件slot来设置下拉触发的元素以及需要通过具名slotdropdown 来设置下拉菜单。默认情况下,下拉按钮只要hover即可,无需点击也会显示下拉菜单。

  1. <el-dropdown>
  2. <span class="el-dropdown-link">
  3. 下拉菜单<i class="el-icon-arrow-down el-icon--right"></i>
  4. </span>
  5. <template #dropdown>
  6. <el-dropdown-menu>
  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 disabled>双皮奶</el-dropdown-item>
  11. <el-dropdown-item divided>蚵仔煎</el-dropdown-item>
  12. </el-dropdown-menu>
  13. </template>
  14. </el-dropdown>
  15. <style>
  16. .el-dropdown-link {
  17. cursor: pointer;
  18. color: #409EFF;
  19. }
  20. .el-icon-arrow-down {
  21. font-size: 12px;
  22. }
  23. </style>

触发对象

可使用按钮触发下拉菜单。

Dropdown 下拉菜单 - 图2

设置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. <template #dropdown>
  6. <el-dropdown-menu>
  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-item>蚵仔煎</el-dropdown-item>
  12. </el-dropdown-menu>
  13. </template>
  14. </el-dropdown>
  15. <el-dropdown split-button type="primary" @click="handleClick">
  16. 更多菜单
  17. <template #dropdown>
  18. <el-dropdown-menu>
  19. <el-dropdown-item>黄金糕</el-dropdown-item>
  20. <el-dropdown-item>狮子头</el-dropdown-item>
  21. <el-dropdown-item>螺蛳粉</el-dropdown-item>
  22. <el-dropdown-item>双皮奶</el-dropdown-item>
  23. <el-dropdown-item>蚵仔煎</el-dropdown-item>
  24. </el-dropdown-menu>
  25. </template>
  26. </el-dropdown>
  27. <style>
  28. .el-dropdown {
  29. vertical-align: top;
  30. }
  31. .el-dropdown + .el-dropdown {
  32. margin-left: 15px;
  33. }
  34. .el-icon-arrow-down {
  35. font-size: 12px;
  36. }
  37. </style>
  38. <script>
  39. export default {
  40. methods: {
  41. handleClick() {
  42. alert('button click');
  43. }
  44. }
  45. }
  46. </script>

触发方式

可以配置 click 激活或者 hover 激活。

Dropdown 下拉菜单 - 图3

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. <template #dropdown>
  9. <el-dropdown-menu>
  10. <el-dropdown-item icon="el-icon-plus">黄金糕</el-dropdown-item>
  11. <el-dropdown-item icon="el-icon-circle-plus">狮子头</el-dropdown-item>
  12. <el-dropdown-item icon="el-icon-circle-plus-outline">螺蛳粉</el-dropdown-item>
  13. <el-dropdown-item icon="el-icon-check">双皮奶</el-dropdown-item>
  14. <el-dropdown-item icon="el-icon-circle-check">蚵仔煎</el-dropdown-item>
  15. </el-dropdown-menu>
  16. </template>
  17. </el-dropdown>
  18. </el-col>
  19. <el-col :span="12">
  20. <span class="demonstration">click 激活</span>
  21. <el-dropdown trigger="click">
  22. <span class="el-dropdown-link">
  23. 下拉菜单<i class="el-icon-arrow-down el-icon--right"></i>
  24. </span>
  25. <template #dropdown>
  26. <el-dropdown-menu>
  27. <el-dropdown-item icon="el-icon-plus">黄金糕</el-dropdown-item>
  28. <el-dropdown-item icon="el-icon-circle-plus">狮子头</el-dropdown-item>
  29. <el-dropdown-item icon="el-icon-circle-plus-outline">螺蛳粉</el-dropdown-item>
  30. <el-dropdown-item icon="el-icon-check">双皮奶</el-dropdown-item>
  31. <el-dropdown-item icon="el-icon-circle-check">蚵仔煎</el-dropdown-item>
  32. </el-dropdown-menu>
  33. </template>
  34. </el-dropdown>
  35. </el-col>
  36. </el-row>
  37. <style>
  38. .el-dropdown-link {
  39. cursor: pointer;
  40. color: #409EFF;
  41. }
  42. .el-icon-arrow-down {
  43. font-size: 12px;
  44. }
  45. .demonstration {
  46. display: block;
  47. color: #8492a6;
  48. font-size: 14px;
  49. margin-bottom: 20px;
  50. }
  51. </style>

菜单隐藏方式

可以hide-on-click属性来配置。

Dropdown 下拉菜单 - 图4

下拉菜单默认在点击菜单项后会被隐藏,将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. <template #dropdown>
  6. <el-dropdown-menu>
  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 disabled>双皮奶</el-dropdown-item>
  11. <el-dropdown-item divided>蚵仔煎</el-dropdown-item>
  12. </el-dropdown-menu>
  13. </template>
  14. </el-dropdown>
  15. <style>
  16. .el-dropdown-link {
  17. cursor: pointer;
  18. color: #409EFF;
  19. }
  20. .el-icon-arrow-down {
  21. font-size: 12px;
  22. }
  23. </style>

指令事件

点击菜单项后会触发事件,用户可以通过相应的菜单项 key 进行不同的操作

Dropdown 下拉菜单 - 图5

  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. <template #dropdown>
  6. <el-dropdown-menu>
  7. <el-dropdown-item command="a">黄金糕</el-dropdown-item>
  8. <el-dropdown-item command="b">狮子头</el-dropdown-item>
  9. <el-dropdown-item command="c">螺蛳粉</el-dropdown-item>
  10. <el-dropdown-item command="d" disabled>双皮奶</el-dropdown-item>
  11. <el-dropdown-item command="e" divided>蚵仔煎</el-dropdown-item>
  12. </el-dropdown-menu>
  13. </template>
  14. </el-dropdown>
  15. <style>
  16. .el-dropdown-link {
  17. cursor: pointer;
  18. color: #409EFF;
  19. }
  20. .el-icon-arrow-down {
  21. font-size: 12px;
  22. }
  23. </style>
  24. <script>
  25. export default {
  26. methods: {
  27. handleCommand(command) {
  28. this.$message('click on item ' + command);
  29. }
  30. }
  31. }
  32. </script>

不同尺寸

Dropdown 组件提供除了默认值以外的三种尺寸,可以在不同场景下选择合适的尺寸。

Dropdown 下拉菜单 - 图6

额外的尺寸:mediumsmallmini,通过设置size属性来配置它们。

  1. <el-dropdown split-button type="primary">
  2. 默认尺寸
  3. <template #dropdown>
  4. <el-dropdown-menu>
  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-item>蚵仔煎</el-dropdown-item>
  10. </el-dropdown-menu>
  11. </template>
  12. </el-dropdown>
  13. <el-dropdown size="medium" split-button type="primary">
  14. 中等尺寸
  15. <template #dropdown>
  16. <el-dropdown-menu>
  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-item>蚵仔煎</el-dropdown-item>
  22. </el-dropdown-menu>
  23. </template>
  24. </el-dropdown>
  25. <el-dropdown size="small" split-button type="primary">
  26. 小型尺寸
  27. <template #dropdown>
  28. <el-dropdown-menu>
  29. <el-dropdown-item>黄金糕</el-dropdown-item>
  30. <el-dropdown-item>狮子头</el-dropdown-item>
  31. <el-dropdown-item>螺蛳粉</el-dropdown-item>
  32. <el-dropdown-item>双皮奶</el-dropdown-item>
  33. <el-dropdown-item>蚵仔煎</el-dropdown-item>
  34. </el-dropdown-menu>
  35. </template>
  36. </el-dropdown>
  37. <el-dropdown size="mini" split-button type="primary">
  38. 超小尺寸
  39. <template #dropdown>
  40. <el-dropdown-menu>
  41. <el-dropdown-item>黄金糕</el-dropdown-item>
  42. <el-dropdown-item>狮子头</el-dropdown-item>
  43. <el-dropdown-item>螺蛳粉</el-dropdown-item>
  44. <el-dropdown-item>双皮奶</el-dropdown-item>
  45. <el-dropdown-item>蚵仔煎</el-dropdown-item>
  46. </el-dropdown-menu>
  47. </template>
  48. </el-dropdown>

Dropdown Attributes

参数说明类型可选值默认值
type菜单按钮类型,同 Button 组件(只在split-button为 true 的情况下有效)string
size菜单尺寸,在split-button为 true 的情况下也对触发按钮生效stringmedium / small / mini
split-button下拉触发元素呈现为按钮组booleanfalse
placement菜单弹出位置stringtop/top-start/top-end/bottom/bottom-start/bottom-endbottom-end
trigger触发下拉的行为stringhover, clickhover
hide-on-click是否在点击菜单项后隐藏菜单booleantrue
show-timeout展开下拉菜单的延时(仅在 trigger 为 hover 时有效)number250
hide-timeout收起下拉菜单的延时(仅在 trigger 为 hover 时有效)number150
tabindexDropdown 组件的 tabindexnumber0

Dropdown Slots

Name说明
触发下拉列表显示的元素。 注意: 必须是一个元素或者或者组件
dropdown下拉列表,通常是 <el-dropdown-menu> 组件

Dropdown Events

事件名称说明回调参数
clicksplit-button 为 true 时,点击左侧按钮的回调
command点击菜单项触发的事件回调dropdown-item 的指令
visible-change下拉框出现/隐藏时触发出现则为 true,隐藏则为 false

Dropdown Menu Item Attributes

参数说明类型可选值默认值
command指令string/number/object
disabled禁用booleanfalse
divided显示分割线booleanfalse
icon图标类名string