037_Dropdown下拉菜单
1. Dropdown下拉菜單
1.1. Dropdown下拉菜單將動作或菜單折疊到下拉菜單中。
1.2. Dropdown Attributes
| 參數(shù) | 說明 | 類型 | 可選值 | 默認值 |
| type | 菜單按鈕類型, 同Button組件(只在split-button為true的情況下有效) | string | 無 | 無 |
| size | 菜單尺寸, 在split-button為true的情況下也對觸發(fā)按鈕生效 | string | medium / small / mini | 無 |
| split-button | 下拉觸發(fā)元素呈現(xiàn)為按鈕組 | boolean | 無 | false |
| placement | 菜單彈出位置 | string | top/top-start/top-end/bottom/bottom-start/bottom-end | bottom-end |
| trigger | 觸發(fā)下拉的行為 | string | hover, click | hover |
| hide-on-click | 是否在點擊菜單項后隱藏菜單 | boolean | 無 | true |
| show-timeout | 展開下拉菜單的延時(僅在trigger為hover時有效) | boolean | 無 | 250 |
| hide-timeout | 收起下拉菜單的延時(僅在trigger為hover時有效) | boolean | 無 | 150 |
| tabindex | Dropdown組件的tabindex | boolean | 無 | 0 |
| disabled | 是否禁用 | boolean | 無 | false |
1.3. Dropdown Slots
| Name | 說明 |
| — | 觸發(fā)下拉列表顯示的元素。 注意: 必須是一個元素或者或者組件 |
| dropdown | 下拉列表, 通常是<el-dropdown-menu>組件 |
1.4. Dropdown Events
| 事件名稱 | 說明 | 回調參數(shù) |
| click | split-button為true時, 點擊左側按鈕的回調 | 無 |
| command | 點擊菜單項觸發(fā)的事件回調 | dropdown-item的指令 |
| visible-change | 下拉框出現(xiàn)/隱藏時觸發(fā) | 出現(xiàn)則為true, 隱藏則為false |
1.5. Dropdown Menu Item Attributes
| 參數(shù) | 說明 | 類型 | 默認值 |
| command | 指令 | string/number/object | 無 |
| disabled | 禁用 | boolean | false |
| divided | 顯示分割線 | boolean | false |
| icon | 圖標類名 | string | 無 |
2. Dropdown下拉菜單例子
2.1. 使用腳手架新建一個名為element-ui-dropdown的前端項目, 同時安裝Element插件。
2.2. 編輯index.js?
import Vue from 'vue' import VueRouter from 'vue-router' import Dropdown from '../components/Dropdown.vue' import ButtonDropdown from '../components/ButtonDropdown.vue' import ClickDropdown from '../components/ClickDropdown.vue' import HideClickDropdown from '../components/HideClickDropdown.vue' import CommandDropdown from '../components/CommandDropdown.vue' import SizeDropdown from '../components/SizeDropdown.vue'Vue.use(VueRouter)const routes = [{ path: '/', redirect: '/Dropdown' },{ path: '/Dropdown', component: Dropdown },{ path: '/ButtonDropdown', component: ButtonDropdown },{ path: '/ClickDropdown', component: ClickDropdown },{ path: '/HideClickDropdown', component: HideClickDropdown },{ path: '/CommandDropdown', component: CommandDropdown },{ path: '/SizeDropdown', component: SizeDropdown } ]const router = new VueRouter({routes })export default router2.3. 在components下創(chuàng)建Dropdown.vue
<template><div><h1>基礎用法</h1><h4>通過組件slot來設置下拉觸發(fā)的元素以及需要通過具名slot為dropdown來設置下拉菜單。默認情況下, 下拉按鈕只要hover即可, 無需點擊也會顯示下拉菜單。</h4><el-dropdown><span class="el-dropdown-link">下拉菜單<i class="el-icon-arrow-down el-icon--right"></i></span><el-dropdown-menu slot="dropdown"><el-dropdown-item>黃金糕</el-dropdown-item><el-dropdown-item>獅子頭</el-dropdown-item><el-dropdown-item>螺螄粉</el-dropdown-item><el-dropdown-item disabled>雙皮奶</el-dropdown-item><el-dropdown-item divided>蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown></div> </template><style scoped>.el-dropdown-link {cursor: pointer;color: #409EFF;}.el-icon-arrow-down {font-size: 12px;} </style>2.4. 在components下創(chuàng)建ButtonDropdown.vue
<template><div><h1>觸發(fā)對象-可使用按鈕觸發(fā)下拉菜單</h1><h4>設置split-button屬性來讓觸發(fā)下拉元素呈現(xiàn)為按鈕組, 左邊是功能按鈕, 右邊是觸發(fā)下拉菜單的按鈕, 設置為true即可。</h4><el-dropdown><el-button type="primary">更多菜單<i class="el-icon-arrow-down el-icon--right"></i></el-button><el-dropdown-menu slot="dropdown"><el-dropdown-item>黃金糕</el-dropdown-item><el-dropdown-item>獅子頭</el-dropdown-item><el-dropdown-item>螺螄粉</el-dropdown-item><el-dropdown-item>雙皮奶</el-dropdown-item><el-dropdown-item>蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown><el-dropdown split-button type="primary" @click="handleClick">更多菜單<el-dropdown-menu slot="dropdown"><el-dropdown-item>黃金糕</el-dropdown-item><el-dropdown-item>獅子頭</el-dropdown-item><el-dropdown-item>螺螄粉</el-dropdown-item><el-dropdown-item>雙皮奶</el-dropdown-item><el-dropdown-item>蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown></div> </template><style scoped>.el-dropdown {vertical-align: top;}.el-dropdown + .el-dropdown {margin-left: 15px;}.el-icon-arrow-down {font-size: 12px;} </style><script> export default {methods: {handleClick () {alert('button click')}} } </script>2.5. 在components下創(chuàng)建ClickDropdown.vue
<template><div><h1>觸發(fā)方式</h1><h4>可以配置click激活或者hover激活。在trigger屬性設置為click即可。</h4><el-row><el-col :span="4"><span class="demonstration">hover 激活</span><el-dropdown><span class="el-dropdown-link">下拉菜單<i class="el-icon-arrow-down el-icon--right"></i></span><el-dropdown-menu slot="dropdown"><el-dropdown-item icon="el-icon-plus">黃金糕</el-dropdown-item><el-dropdown-item icon="el-icon-circle-plus">獅子頭</el-dropdown-item><el-dropdown-item icon="el-icon-circle-plus-outline">螺螄粉</el-dropdown-item><el-dropdown-item icon="el-icon-check">雙皮奶</el-dropdown-item><el-dropdown-item icon="el-icon-circle-check">蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown></el-col><el-col :span="4"><span class="demonstration">click 激活</span><el-dropdown trigger="click"><span class="el-dropdown-link">下拉菜單<i class="el-icon-arrow-down el-icon--right"></i></span><el-dropdown-menu slot="dropdown"><el-dropdown-item icon="el-icon-plus">黃金糕</el-dropdown-item><el-dropdown-item icon="el-icon-circle-plus">獅子頭</el-dropdown-item><el-dropdown-item icon="el-icon-circle-plus-outline">螺螄粉</el-dropdown-item><el-dropdown-item icon="el-icon-check">雙皮奶</el-dropdown-item><el-dropdown-item icon="el-icon-circle-check">蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown></el-col></el-row></div> </template><style scoped>.el-dropdown-link {cursor: pointer;color: #409EFF;}.el-icon-arrow-down {font-size: 12px;}.demonstration {display: block;color: #8492a6;font-size: 14px;margin-bottom: 20px;} </style>2.6. 在components下創(chuàng)建HideClickDropdown.vue
<template><div><h1>菜單隱藏方式</h1><h4>下拉菜單默認在點擊菜單項后會被隱藏, 將hide-on-click屬性默認為false可以關閉此功能。</h4><el-dropdown :hide-on-click="false"><span class="el-dropdown-link">下拉菜單<i class="el-icon-arrow-down el-icon--right"></i></span><el-dropdown-menu slot="dropdown"><el-dropdown-item>黃金糕</el-dropdown-item><el-dropdown-item>獅子頭</el-dropdown-item><el-dropdown-item>螺螄粉</el-dropdown-item><el-dropdown-item disabled>雙皮奶</el-dropdown-item><el-dropdown-item divided>蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown></div> </template><style scoped>.el-dropdown-link {cursor: pointer;color: #409EFF;}.el-icon-arrow-down {font-size: 12px;} </style>2.7. 在components下創(chuàng)建CommandDropdown.vue
<template><div><h1>指令事件</h1><h4>點擊菜單項后會觸發(fā)事件, 用戶可以通過相應的菜單項key進行不同的操作。</h4><el-dropdown @command="handleCommand"><span class="el-dropdown-link">下拉菜單<i class="el-icon-arrow-down el-icon--right"></i></span><el-dropdown-menu slot="dropdown"><el-dropdown-item command="a">黃金糕</el-dropdown-item><el-dropdown-item command="b">獅子頭</el-dropdown-item><el-dropdown-item command="c">螺螄粉</el-dropdown-item><el-dropdown-item command="d" disabled>雙皮奶</el-dropdown-item><el-dropdown-item command="e" divided>蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown></div> </template><style scoped>.el-dropdown-link {cursor: pointer;color: #409EFF;}.el-icon-arrow-down {font-size: 12px;} </style><script> export default {methods: {handleCommand (command) {this.$message('click on item ' + command)}} } </script>2.8. 在components下創(chuàng)建SizeDropdown.vue
<template><div><h1>不同尺寸</h1><h4>Dropdown組件提供除了默認值以外的三種尺寸, 可以在不同場景下選擇合適的尺寸。額外的尺寸: medium、small、mini, 通過設置size屬性來配置它們。</h4><el-dropdown split-button type="primary">默認尺寸<el-dropdown-menu slot="dropdown"><el-dropdown-item>黃金糕</el-dropdown-item><el-dropdown-item>獅子頭</el-dropdown-item><el-dropdown-item>螺螄粉</el-dropdown-item><el-dropdown-item>雙皮奶</el-dropdown-item><el-dropdown-item>蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown><el-dropdown size="medium" split-button type="primary">中等尺寸<el-dropdown-menu slot="dropdown"><el-dropdown-item>黃金糕</el-dropdown-item><el-dropdown-item>獅子頭</el-dropdown-item><el-dropdown-item>螺螄粉</el-dropdown-item><el-dropdown-item>雙皮奶</el-dropdown-item><el-dropdown-item>蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown><el-dropdown size="small" split-button type="primary">小型尺寸<el-dropdown-menu slot="dropdown"><el-dropdown-item>黃金糕</el-dropdown-item><el-dropdown-item>獅子頭</el-dropdown-item><el-dropdown-item>螺螄粉</el-dropdown-item><el-dropdown-item>雙皮奶</el-dropdown-item><el-dropdown-item>蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown><el-dropdown size="mini" split-button type="primary">超小尺寸<el-dropdown-menu slot="dropdown"><el-dropdown-item>黃金糕</el-dropdown-item><el-dropdown-item>獅子頭</el-dropdown-item><el-dropdown-item>螺螄粉</el-dropdown-item><el-dropdown-item>雙皮奶</el-dropdown-item><el-dropdown-item>蚵仔煎</el-dropdown-item></el-dropdown-menu></el-dropdown></div> </template><style scoped>.el-dropdown + .el-dropdown {margin-left: 15px;}.el-icon-arrow-down {font-size: 12px;} </style>2.9. 運行項目, 訪問http://localhost:8080/#/Dropdown
2.10. 運行項目, 訪問http://localhost:8080/#/ButtonDropdown
2.11. 運行項目, 訪問http://localhost:8080/#/ClickDropdown
2.12. 運行項目, 訪問http://localhost:8080/#/HideClickDropdown
2.13. 運行項目, 訪問http://localhost:8080/#/CommandDropdown
2.14. 運行項目, 訪問http://localhost:8080/#/SizeDropdown
總結
以上是生活随笔為你收集整理的037_Dropdown下拉菜单的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 035_Breadcrumb面包屑
- 下一篇: 039_Dialog对话框