js-components-basic-menu.md 11.6 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# menu<a name="ZH-CN_TOPIC_0000001173164715"></a>

提供菜单组件,作为临时性弹出窗口,用于展示用户可执行的操作。

## 权限列表<a name="section11257113618419"></a>



## 子组件<a name="section9288143101012"></a>

<[option](js-components-basic-option.md)\>子组件。

## 属性<a name="section2907183951110"></a>

除支持[通用属性](js-components-common-attributes.md)外,还支持如下属性:↵

Z
zengyawen 已提交
17
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="23.119999999999997%" id="mcps1.1.6.1.1"><p>名称</p>
Z
zengyawen 已提交
18
</th>
Z
zengyawen 已提交
19
<th class="cellrowborder" valign="top" width="23.119999999999997%" id="mcps1.1.6.1.2"><p>类型</p>
Z
zengyawen 已提交
20
</th>
Z
zengyawen 已提交
21
<th class="cellrowborder" valign="top" width="10.48%" id="mcps1.1.6.1.3"><p>默认值</p>
Z
zengyawen 已提交
22
</th>
Z
zengyawen 已提交
23
<th class="cellrowborder" valign="top" width="7.5200000000000005%" id="mcps1.1.6.1.4"><p>必填</p>
Z
zengyawen 已提交
24
</th>
Z
zengyawen 已提交
25
<th class="cellrowborder" valign="top" width="35.76%" id="mcps1.1.6.1.5"><p>描述</p>
Z
zengyawen 已提交
26 27 28
</th>
</tr>
</thead>
Z
zengyawen 已提交
29
<tbody><tr><td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.1 "><p>target</p>
Z
zengyawen 已提交
30
</td>
Z
zengyawen 已提交
31
<td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
32
</td>
Z
zengyawen 已提交
33
<td class="cellrowborder" valign="top" width="10.48%" headers="mcps1.1.6.1.3 "><p>-</p>
Z
zengyawen 已提交
34
</td>
Z
zengyawen 已提交
35
<td class="cellrowborder" valign="top" width="7.5200000000000005%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
36
</td>
Z
zengyawen 已提交
37
<td class="cellrowborder" valign="top" width="35.76%" headers="mcps1.1.6.1.5 "><p>目标元素选择器。当使用目标元素选择器后,点击目标元素会自动弹出menu菜单。弹出菜单位置优先为目标元素右下角,当右边可视空间不足时会适当左移,当下方空间不足时会适当上移。</p>
Z
zengyawen 已提交
38 39
</td>
</tr>
Z
zengyawen 已提交
40
<tr><td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.1 "><p>type</p>
Z
zengyawen 已提交
41
</td>
Z
zengyawen 已提交
42
<td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
43
</td>
Z
zengyawen 已提交
44
<td class="cellrowborder" valign="top" width="10.48%" headers="mcps1.1.6.1.3 "><p>click</p>
Z
zengyawen 已提交
45
</td>
Z
zengyawen 已提交
46
<td class="cellrowborder" valign="top" width="7.5200000000000005%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
47
</td>
Z
zengyawen 已提交
48 49
<td class="cellrowborder" valign="top" width="35.76%" headers="mcps1.1.6.1.5 "><p>目标元素触发弹窗的方式,可选值有:</p>
<ul><li>click:点击弹窗。</li><li>longpress:长按弹窗。</li></ul>
Z
zengyawen 已提交
50 51
</td>
</tr>
Z
zengyawen 已提交
52
<tr><td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.1 "><p>title</p>
Z
zengyawen 已提交
53
</td>
Z
zengyawen 已提交
54
<td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
55
</td>
Z
zengyawen 已提交
56
<td class="cellrowborder" valign="top" width="10.48%" headers="mcps1.1.6.1.3 "><p>-</p>
Z
zengyawen 已提交
57
</td>
Z
zengyawen 已提交
58
<td class="cellrowborder" valign="top" width="7.5200000000000005%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
59
</td>
Z
zengyawen 已提交
60
<td class="cellrowborder" valign="top" width="35.76%" headers="mcps1.1.6.1.5 "><p>菜单标题内容。</p>
Z
zengyawen 已提交
61 62 63 64 65 66
</td>
</tr>
</tbody>
</table>

>![](../../public_sys-resources/icon-note.gif) **说明:** 
Z
zengyawen 已提交
67
>不支持focusable、disabled属性。
Z
zengyawen 已提交
68 69 70 71 72

## 样式<a name="section1382826121311"></a>

仅支持如下样式:

Z
zengyawen 已提交
73
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="19.038096190380962%" id="mcps1.1.6.1.1"><p>名称</p>
Z
zengyawen 已提交
74
</th>
Z
zengyawen 已提交
75
<th class="cellrowborder" valign="top" width="23.847615238476152%" id="mcps1.1.6.1.2"><p>类型</p>
Z
zengyawen 已提交
76
</th>
Z
zengyawen 已提交
77
<th class="cellrowborder" valign="top" width="12.118788121187881%" id="mcps1.1.6.1.3"><p>默认值</p>
Z
zengyawen 已提交
78
</th>
Z
zengyawen 已提交
79
<th class="cellrowborder" valign="top" width="8.269173082691731%" id="mcps1.1.6.1.4"><p>必填</p>
Z
zengyawen 已提交
80
</th>
Z
zengyawen 已提交
81
<th class="cellrowborder" valign="top" width="36.72632736726327%" id="mcps1.1.6.1.5"><p>描述</p>
Z
zengyawen 已提交
82 83 84
</th>
</tr>
</thead>
Z
zengyawen 已提交
85
<tbody><tr><td class="cellrowborder" valign="top" width="19.038096190380962%" headers="mcps1.1.6.1.1 "><p>text-color</p>
Z
zengyawen 已提交
86
</td>
Z
zengyawen 已提交
87
<td class="cellrowborder" valign="top" width="23.847615238476152%" headers="mcps1.1.6.1.2 "><p>&lt;color&gt;</p>
Z
zengyawen 已提交
88
</td>
Z
zengyawen 已提交
89
<td class="cellrowborder" valign="top" width="12.118788121187881%" headers="mcps1.1.6.1.3 "><p>-</p>
Z
zengyawen 已提交
90
</td>
Z
zengyawen 已提交
91
<td class="cellrowborder" valign="top" width="8.269173082691731%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
92
</td>
Z
zengyawen 已提交
93
<td class="cellrowborder" valign="top" width="36.72632736726327%" headers="mcps1.1.6.1.5 "><p>设置菜单的文本颜色。</p>
Z
zengyawen 已提交
94 95
</td>
</tr>
Z
zengyawen 已提交
96
<tr><td class="cellrowborder" valign="top" width="19.038096190380962%" headers="mcps1.1.6.1.1 "><p>font-size</p>
Z
zengyawen 已提交
97
</td>
Z
zengyawen 已提交
98
<td class="cellrowborder" valign="top" width="23.847615238476152%" headers="mcps1.1.6.1.2 "><p>&lt;length&gt;</p>
Z
zengyawen 已提交
99
</td>
Z
zengyawen 已提交
100
<td class="cellrowborder" valign="top" width="12.118788121187881%" headers="mcps1.1.6.1.3 "><p>30px</p>
Z
zengyawen 已提交
101
</td>
Z
zengyawen 已提交
102
<td class="cellrowborder" valign="top" width="8.269173082691731%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
103
</td>
Z
zengyawen 已提交
104
<td class="cellrowborder" valign="top" width="36.72632736726327%" headers="mcps1.1.6.1.5 "><p>设置菜单的文本尺寸。</p>
Z
zengyawen 已提交
105 106
</td>
</tr>
Z
zengyawen 已提交
107
<tr><td class="cellrowborder" valign="top" width="19.038096190380962%" headers="mcps1.1.6.1.1 "><p>allow-scale</p>
Z
zengyawen 已提交
108
</td>
Z
zengyawen 已提交
109
<td class="cellrowborder" valign="top" width="23.847615238476152%" headers="mcps1.1.6.1.2 "><p>boolean</p>
Z
zengyawen 已提交
110
</td>
Z
zengyawen 已提交
111
<td class="cellrowborder" valign="top" width="12.118788121187881%" headers="mcps1.1.6.1.3 "><p>true</p>
Z
zengyawen 已提交
112
</td>
Z
zengyawen 已提交
113
<td class="cellrowborder" valign="top" width="8.269173082691731%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
114
</td>
Z
zengyawen 已提交
115 116
<td class="cellrowborder" valign="top" width="36.72632736726327%" headers="mcps1.1.6.1.5 "><p>设置菜单的文本尺寸是否跟随系统设置字体缩放尺寸进行放大缩小。</p>
<div class="note"><span class="notetitle"> 说明: </span><div class="notebody"><p>如果在config描述文件中针对ability配置了fontSize的config-changes标签,则应用不会重启而直接生效。</p>
Z
zengyawen 已提交
117 118 119
</div></div>
</td>
</tr>
Z
zengyawen 已提交
120
<tr><td class="cellrowborder" valign="top" width="19.038096190380962%" headers="mcps1.1.6.1.1 "><p>letter-spacing</p>
Z
zengyawen 已提交
121
</td>
Z
zengyawen 已提交
122
<td class="cellrowborder" valign="top" width="23.847615238476152%" headers="mcps1.1.6.1.2 "><p>&lt;length&gt;</p>
Z
zengyawen 已提交
123
</td>
Z
zengyawen 已提交
124
<td class="cellrowborder" valign="top" width="12.118788121187881%" headers="mcps1.1.6.1.3 "><p>0</p>
Z
zengyawen 已提交
125
</td>
Z
zengyawen 已提交
126
<td class="cellrowborder" valign="top" width="8.269173082691731%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
127
</td>
Z
zengyawen 已提交
128
<td class="cellrowborder" valign="top" width="36.72632736726327%" headers="mcps1.1.6.1.5 "><p>设置菜单的字符间距。</p>
Z
zengyawen 已提交
129 130
</td>
</tr>
Z
zengyawen 已提交
131
<tr><td class="cellrowborder" valign="top" width="19.038096190380962%" headers="mcps1.1.6.1.1 "><p>font-style</p>
Z
zengyawen 已提交
132
</td>
Z
zengyawen 已提交
133
<td class="cellrowborder" valign="top" width="23.847615238476152%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
134
</td>
Z
zengyawen 已提交
135
<td class="cellrowborder" valign="top" width="12.118788121187881%" headers="mcps1.1.6.1.3 "><p>normal</p>
Z
zengyawen 已提交
136
</td>
Z
zengyawen 已提交
137
<td class="cellrowborder" valign="top" width="8.269173082691731%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
138
</td>
Z
zengyawen 已提交
139
<td class="cellrowborder" valign="top" width="36.72632736726327%" headers="mcps1.1.6.1.5 "><p>设置菜单的字体样式。见<a href="js-components-basic-text.md#section5775351116">text组件font-style的样式属性</a></p>
Z
zengyawen 已提交
140 141
</td>
</tr>
Z
zengyawen 已提交
142
<tr><td class="cellrowborder" valign="top" width="19.038096190380962%" headers="mcps1.1.6.1.1 "><p>font-weight</p>
Z
zengyawen 已提交
143
</td>
Z
zengyawen 已提交
144
<td class="cellrowborder" valign="top" width="23.847615238476152%" headers="mcps1.1.6.1.2 "><p>number | string</p>
Z
zengyawen 已提交
145
</td>
Z
zengyawen 已提交
146
<td class="cellrowborder" valign="top" width="12.118788121187881%" headers="mcps1.1.6.1.3 "><p>normal</p>
Z
zengyawen 已提交
147
</td>
Z
zengyawen 已提交
148
<td class="cellrowborder" valign="top" width="8.269173082691731%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
149
</td>
Z
zengyawen 已提交
150
<td class="cellrowborder" valign="top" width="36.72632736726327%" headers="mcps1.1.6.1.5 "><p>设置菜单的字体粗细。见<a href="js-components-basic-text.md#section5775351116">text组件font-weight的样式属性</a></p>
Z
zengyawen 已提交
151 152
</td>
</tr>
Z
zengyawen 已提交
153
<tr><td class="cellrowborder" valign="top" width="19.038096190380962%" headers="mcps1.1.6.1.1 "><p>font-family</p>
Z
zengyawen 已提交
154
</td>
Z
zengyawen 已提交
155
<td class="cellrowborder" valign="top" width="23.847615238476152%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
156
</td>
Z
zengyawen 已提交
157
<td class="cellrowborder" valign="top" width="12.118788121187881%" headers="mcps1.1.6.1.3 "><p>sans-serif</p>
Z
zengyawen 已提交
158
</td>
Z
zengyawen 已提交
159
<td class="cellrowborder" valign="top" width="8.269173082691731%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
160
</td>
Z
zengyawen 已提交
161
<td class="cellrowborder" valign="top" width="36.72632736726327%" headers="mcps1.1.6.1.5 "><p>设置菜单的字体列表,用逗号分隔,每个字体用字体名或者字体族名设置。列表中第一个系统中存在的或者通过<a href="js-components-common-customizing-font.md">自定义字体</a>指定的字体,会被选中作为文本的字体。</p>
Z
zengyawen 已提交
162 163 164 165 166 167 168 169 170
</td>
</tr>
</tbody>
</table>

## 事件<a name="section5624519222"></a>

仅支持如下事件:

Z
zengyawen 已提交
171
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="18.459999999999997%" id="mcps1.1.4.1.1"><p>名称</p>
Z
zengyawen 已提交
172
</th>
Z
zengyawen 已提交
173
<th class="cellrowborder" valign="top" width="30.769999999999996%" id="mcps1.1.4.1.2"><p>参数</p>
Z
zengyawen 已提交
174
</th>
Z
zengyawen 已提交
175
<th class="cellrowborder" valign="top" width="50.77%" id="mcps1.1.4.1.3"><p>描述</p>
Z
zengyawen 已提交
176 177 178
</th>
</tr>
</thead>
Z
zengyawen 已提交
179
<tbody><tr><td class="cellrowborder" valign="top" width="18.459999999999997%" headers="mcps1.1.4.1.1 "><p>selected</p>
Z
zengyawen 已提交
180
</td>
Z
zengyawen 已提交
181
<td class="cellrowborder" valign="top" width="30.769999999999996%" headers="mcps1.1.4.1.2 "><p>{ value:value }</p>
Z
zengyawen 已提交
182
</td>
Z
zengyawen 已提交
183
<td class="cellrowborder" valign="top" width="50.77%" headers="mcps1.1.4.1.3 "><p>菜单中某个值被点击选中时触发,返回的value值为option组件的value属性。</p>
Z
zengyawen 已提交
184 185
</td>
</tr>
Z
zengyawen 已提交
186
<tr><td class="cellrowborder" valign="top" width="18.459999999999997%" headers="mcps1.1.4.1.1 "><p>cancel</p>
Z
zengyawen 已提交
187
</td>
Z
zengyawen 已提交
188
<td class="cellrowborder" valign="top" width="30.769999999999996%" headers="mcps1.1.4.1.2 "><p>-</p>
Z
zengyawen 已提交
189
</td>
Z
zengyawen 已提交
190
<td class="cellrowborder" valign="top" width="50.77%" headers="mcps1.1.4.1.3 "><p>用户取消。</p>
Z
zengyawen 已提交
191 192 193 194 195 196 197 198 199
</td>
</tr>
</tbody>
</table>

## 方法<a name="section47669296127"></a>

仅支持如下方法。

Z
zengyawen 已提交
200
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="18.459999999999997%" id="mcps1.1.4.1.1"><p>名称</p>
Z
zengyawen 已提交
201
</th>
Z
zengyawen 已提交
202
<th class="cellrowborder" valign="top" width="30.769999999999996%" id="mcps1.1.4.1.2"><p>参数</p>
Z
zengyawen 已提交
203
</th>
Z
zengyawen 已提交
204
<th class="cellrowborder" valign="top" width="50.77%" id="mcps1.1.4.1.3"><p>描述</p>
Z
zengyawen 已提交
205 206 207
</th>
</tr>
</thead>
Z
zengyawen 已提交
208
<tbody><tr><td class="cellrowborder" valign="top" width="18.459999999999997%" headers="mcps1.1.4.1.1 "><p>show</p>
Z
zengyawen 已提交
209
</td>
Z
zengyawen 已提交
210
<td class="cellrowborder" valign="top" width="30.769999999999996%" headers="mcps1.1.4.1.2 "><p>{ x:x,  y:y }</p>
Z
zengyawen 已提交
211
</td>
Z
zengyawen 已提交
212
<td class="cellrowborder" valign="top" width="50.77%" headers="mcps1.1.4.1.3 "><p>显示menu菜单。(x, y)指定菜单弹窗位置。其中x表示距离可见区域左边沿的 X 轴坐标,不包含任何滚动偏移,y表示距离可见区域上边沿的 Y 轴坐标,不包含任何滚动偏移以及状态栏。菜单优先显示在弹窗位置右下角,当右边可视空间不足时会适当左移,当下方空间不足时会适当上移。</p>
Z
zengyawen 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
</td>
</tr>
</tbody>
</table>

## 示例<a name="section54636714136"></a>

```
<!-- xxx.hml -->
<div class="container">
  <text onclick="onTextClick" class="title-text">Show popup menu.</text>
  <menu id="apiMenu" onselected="onMenuSelected">
    <option value="Item 1">Item 1</option>
    <option value="Item 2">Item 2</option>
    <option value="Item 3">Item 3</option>
  </menu>
</div>
```

```
/* xxx.css */
.container {
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}
.title-text {
  margin: 20px;
}
```

```
// xxx.js
import prompt from '@system.prompt';
export default {
  onMenuSelected(e) {
    prompt.showToast({
      message: e.value
    })
  },
  onTextClick() {
    this.$element("apiMenu").show({x:280,y:120});
  }
}
```

![](figures/menu13.gif)