naive-ui/demo/documentation/components/dropdown/zhCN/manualPosition.md

94 lines
1.7 KiB
Markdown
Raw Normal View History

2019-12-14 02:05:31 +08:00
# 手动定位
在特殊情况下,你可能想手动定位下拉菜单。比如在一块区域右击以弹出下拉菜单。
```html
<div style="width: 200px; height: 200px; background-color: rgba(0, 128, 0, .5);" @contextmenu="handleContextMenu">
右击
</div>
<n-dropdown
placement="bottom-start"
trigger="manual"
manually-positioned
@select="handleSelect"
@blur="handleBlur"
:x="x"
:y="y"
2020-02-04 22:07:55 +08:00
:options="options"
:show="showDropdown"
/>
2019-12-14 02:05:31 +08:00
```
```js
2020-02-04 22:07:55 +08:00
const options = [
{
label: '杰·盖茨比',
value: 'jay gatsby'
2020-02-04 22:07:55 +08:00
},
{
label: '黛西·布坎南',
value: 'daisy buchanan'
2020-02-04 22:07:55 +08:00
},
{
type: 'divider'
},
{
label: '尼克·卡拉威',
value: 'nick carraway'
2020-02-04 22:07:55 +08:00
},
{
label: '其他',
value: 'others',
2020-02-04 22:07:55 +08:00
children: [
{
label: '乔丹·贝克',
value: 'jordan baker'
2020-02-04 22:07:55 +08:00
},
{
label: '汤姆·布坎南',
value: 'tom buchanan'
2020-02-04 22:07:55 +08:00
},
{
label: '其他',
value: 'others',
2020-02-04 22:07:55 +08:00
children: [
{
label: '鸡肉',
value: 'chicken'
2020-02-04 22:07:55 +08:00
},
{
label: '牛肉',
value: 'beef'
2020-02-04 22:07:55 +08:00
}
]
}
]
}
]
2019-12-14 02:05:31 +08:00
export default {
methods: {
handleSelect (name) {
this.showDropdown = false
this.$NMessage.info(name)
},
handleBlur () {
this.showDropdown = false
},
handleContextMenu (e) {
e.preventDefault()
this.showDropdown = false
this.$nextTick().then(() => {
this.showDropdown = true
this.x = e.clientX
this.y = e.clientY
})
}
},
data () {
return {
2020-02-04 22:07:55 +08:00
options,
2019-12-14 02:05:31 +08:00
showDropdown: false,
x: 0,
y: 0
}
}
}
2020-02-04 22:07:55 +08:00
```