Popover 气泡菜单
10:27说明
在触发元素旁弹出菜单面板。菜单内容全部由 #menu 插槽提供,组件不内置菜单项渲染,也不预设菜单项样式;显隐用 v-model:show 控制,placement 六档方位决定面板落在触发元素上方还是下方、以及左 / 中 / 右对齐。
打开后面板自动贴合触发元素并收敛在视口内:纵向放不下时翻到另一侧,横向贴边时往屏幕内侧推。Web 端打开期间滚动页面会自动关闭,App 与小程序端由遮罩阻断触摸滑动。面板内容尺寸异步变化后,改 refreshKey 或调用 refresh() 重测;窗口尺寸变化会自动重测一次。
外观默认「1px 描边 + 单层柔和阴影」,箭头与面板同底色、无缝衔接;show-border 与 show-shadow 可组合出从描边卡片到微信风格纯色气泡的样式,底色用 bg-color 调整。更细的边界规则见「注意事项」。
兼容性
| 平台 | 支持情况 |
|---|---|
| App | ✅ 支持(不监听页面滚动,靠遮罩阻断触摸滚动;代码主动 scrollTo 时面板不会自动关闭,需自行 close() 或 refresh()) |
| H5 | ✅ 支持(打开期间任何滚动都会自动关闭;首帧测位依赖 nextTick + 50ms 节拍) |
| 微信小程序 | ✅ 支持(同 App:遮罩拦截触摸滚动;首帧测位需节拍,面板 max-height + overflow-y 兜底生效) |
示例
基础用法
点按钮在下方右侧展开菜单,点任意菜单项由调用方把 show 置 false 关闭。
查看源码
<template>
<view class="popover-row">
<ux-popover v-model:show="show" placement="bottom-end">
<ux-button shape="round" size="small" type="primary" text="点我展开" @click="show = true"></ux-button>
<template #menu>
<view class="popover-menu">
<ux-cell-item title="编辑资料" :arrow="false" @click="onPick()"></ux-cell-item>
<ux-cell-item title="复制链接" :arrow="false" @click="onPick()"></ux-cell-item>
<ux-cell-item title="删除" :arrow="false" :border-bottom="false" @click="onPick()"></ux-cell-item>
</view>
</template>
</ux-popover>
</view>
</template>
<script setup lang="ts">
const show = ref<boolean>(false);
// 点菜单项后由调用方自己关:组件不猜插槽里的点击语义
function onPick(): void {
show.value = false;
}
</script>
<style lang="scss">
/* 触发区必须是 flex 容器:组件只给触发区 align-self: flex-start,非 flex 父级会让它占满一行 */
.popover-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
}
/* 面板宽度由内容撑开,给菜单一个最小宽度 */
.popover-menu {
min-width: 180px;
}
</style>六个方位
六个按钮各自对应一个独立实例,逐个点开对照箭头朝向与对齐点。
查看源码
<template>
<view class="popover-grid">
<view class="popover-grid-cell">
<ux-popover v-model:show="bottomEndShow" placement="bottom-end">
<ux-button shape="round" size="small" text="下方-末" @click="bottomEndShow = true"></ux-button>
<template #menu>
<view class="popover-menu--compact">
<text class="popover-menu-text">placement = bottom-end</text>
</view>
</template>
</ux-popover>
</view>
<view class="popover-grid-cell">
<ux-popover v-model:show="topShow" placement="top">
<ux-button shape="round" size="small" text="上方-中" @click="topShow = true"></ux-button>
<template #menu>
<view class="popover-menu--compact">
<text class="popover-menu-text">placement = top</text>
</view>
</template>
</ux-popover>
</view>
<!-- bottom / bottom-start / top-end / top-start 同理,一个方位一个实例 -->
</view>
</template>
<script setup lang="ts">
// 触发元素必须落在自己实例的默认插槽里,所以一个方位一个实例
const bottomEndShow = ref<boolean>(false);
const topShow = ref<boolean>(false);
</script>
<style lang="scss">
/* 三等分格子:按钮宽度跟内容,格子里居中 */
.popover-grid {
display: flex;
flex-direction: row;
flex-wrap: wrap;
margin: 0 -4px;
}
.popover-grid-cell {
box-sizing: border-box;
width: 33.3333%;
padding: 4px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.popover-menu--compact {
min-width: 140px;
padding: 10px 14px;
}
.popover-menu-text {
/* #ifndef APP */
display: block;
/* #endif */
font-size: 12px;
}
</style>箭头与外观
开关与档位改完再点「按当前外观打开」,面板内的回显行就是本例真正生效的取值。
查看源码
<template>
<ux-cell-group>
<ux-cell-item title="显示箭头" :arrow="false">
<ux-switch v-model="arrowOn"></ux-switch>
</ux-cell-item>
<ux-cell-item title="显示描边" :arrow="false">
<ux-switch v-model="borderOn"></ux-switch>
</ux-cell-item>
<ux-cell-item title="箭头边长" :arrow="false">
<view class="popover-segments">
<ux-button
v-for="(item, index) in sizeList"
:key="index"
:type="arrowSize == item.value ? 'primary' : 'default'"
custom-class="popover-segment"
size="small"
shape="round"
:text="item.label"
@click="arrowSize = item.value"
></ux-button>
</view>
</ux-cell-item>
<ux-cell-item title="面板圆角" :arrow="false" :border-bottom="false">
<view class="popover-segments">
<ux-button
v-for="(item, index) in radiusList"
:key="index"
:type="radius == item.value ? 'primary' : 'default'"
custom-class="popover-segment"
size="small"
shape="round"
:text="item.label"
@click="radius = item.value"
></ux-button>
</view>
</ux-cell-item>
</ux-cell-group>
<!-- 动作单独成行:和档位胶囊排在一起就看不出哪个是参数、哪个是动作 -->
<ux-gap :height="12" />
<view class="popover-row">
<ux-popover
v-model:show="show"
:arrow-size="arrowSize"
:border-radius="radius"
:show-arrow="arrowOn"
:show-border="borderOn"
placement="bottom-start"
>
<ux-button shape="round" size="small" type="primary" text="按当前外观打开" @click="show = true"></ux-button>
<template #menu>
<view class="popover-menu">
<view class="popover-menu-body">
<text class="popover-menu-text">arrow-size = {{ arrowSize }}</text>
<text class="popover-menu-text">border-radius = {{ radius }}</text>
</view>
<!-- 按钮包进带内边距的容器:block 的 100% 只算容器内容宽,直接叠 margin 会顶破面板右边缘 -->
<view class="popover-menu-footer">
<ux-button block size="small" shape="round" text="关闭" @click="show = false"></ux-button>
</view>
</view>
</template>
</ux-popover>
</view>
</template>
<script setup lang="ts">
const show = ref<boolean>(false);
const arrowOn = ref<boolean>(true);
const borderOn = ref<boolean>(true);
const arrowSize = ref<number>(8);
const radius = ref<number>(8);
const sizeList: { label: string; value: number }[] = [
{ label: '6', value: 6 },
{ label: '8', value: 8 },
{ label: '12', value: 12 }
];
const radiusList: { label: string; value: number }[] = [
{ label: '0', value: 0 },
{ label: '8', value: 8 },
{ label: '16', value: 16 }
];
</script>
<style lang="scss">
/* 触发按钮必须在 flex 容器里:组件只给触发区 align-self: flex-start */
.popover-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
}
/* 档位分段贴单元格右侧,项间 6px */
.popover-segments {
display: flex;
flex-direction: row;
align-items: center;
}
.popover-segment {
margin-left: 6px;
}
.popover-menu {
min-width: 180px;
}
.popover-menu-body {
padding: 10px 14px;
}
/* 菜单底部动作区:内边距留给容器,里面的 block 按钮才是安全的 100% 宽 */
.popover-menu-footer {
padding: 6px 14px 12px 14px;
}
.popover-menu-text {
/* #ifndef APP */
display: block;
/* #endif */
font-size: 12px;
line-height: 1.8;
}
</style>微信风格气泡
固定深灰底、无描边、无阴影、小圆角,箭头与面板完全融为一体;深底菜单的文字颜色需由插槽内容自己给。
查看源码
<template>
<view class="popover-row">
<ux-popover
v-model:show="show"
bg-color="#4c4c4c"
placement="bottom-end"
:arrow-size="12"
:border-radius="8"
:offset="10"
:show-border="false"
:show-shadow="false"
>
<ux-button shape="round" size="small" type="primary" text="点我展开" @click="show = true"></ux-button>
<template #menu>
<view class="popover-wechat-menu">
<view v-for="(item, index) in items" :key="index" class="popover-wechat-item" @click="onPick()">
<view class="popover-wechat-icon">
<ux-icon :name="item.icon" size="22" color="#ffffff"></ux-icon>
</view>
<text class="popover-wechat-text">{{ item.label }}</text>
</view>
</view>
</template>
</ux-popover>
</view>
</template>
<script setup lang="ts">
const show = ref<boolean>(false);
const items: { icon: string; label: string }[] = [
{ icon: 'chat-fill', label: '发起群聊' },
{ icon: 'account-fill', label: '添加朋友' },
{ icon: 'scan', label: '扫一扫' },
{ icon: 'rmb', label: '收付款' }
];
// 气泡式菜单选完即关
function onPick(): void {
show.value = false;
}
</script>
<style lang="scss">
.popover-row {
display: flex;
flex-direction: row;
align-items: center;
}
.popover-wechat-menu {
min-width: 180px;
padding: 6px 0;
}
.popover-wechat-item {
display: flex;
flex-direction: row;
align-items: center;
padding: 12px 18px;
}
/* 图标固定占位:同一个横向基线,文字不会因为图标宽窄而错位 */
.popover-wechat-icon {
display: flex;
align-items: center;
justify-content: center;
width: 22px;
}
/* 深底白字:颜色必须落在 text 自身,App 端 text 不继承父级颜色 */
.popover-wechat-text {
/* #ifndef APP */
display: block;
/* #endif */
font-size: 14px;
color: #ffffff;
margin-left: 12px;
}
</style>评论操作气泡
横排「图标 + 文字」操作项,面板在触发元素上方:无描边、无阴影,三角与面板同色;每项宽度由插槽内容按 flex 均分。
查看源码
<template>
<view class="popover-comment">
<view class="popover-comment-main">
<text class="popover-comment-name">前端梦工厂</text>
<text class="popover-comment-text">TypeScript 2.0 版已发布,欢迎体验。</text>
</view>
<ux-popover
v-model:show="show"
bg-color="#3d3d3d"
placement="top-end"
:arrow-size="14"
:border-radius="10"
:offset="10"
:show-border="false"
:show-shadow="false"
>
<view class="popover-more" @click="show = true">
<ux-icon name="more-dot-fill" size="20" color="#909399"></ux-icon>
</view>
<template #menu>
<view class="popover-action-bar">
<view v-for="(item, index) in actions" :key="index" class="popover-action-item" @click="onPick()">
<ux-icon :name="item.icon" size="22" color="#ffffff"></ux-icon>
<text class="popover-action-text">{{ item.label }}</text>
</view>
</view>
</template>
</ux-popover>
</view>
</template>
<script setup lang="ts">
const show = ref<boolean>(false);
const actions: { icon: string; label: string }[] = [
{ icon: 'file-text', label: '复制' },
{ icon: 'share', label: '转发' },
{ icon: 'chat', label: '翻译' },
{ icon: 'heart', label: '不喜欢' },
{ icon: 'warning', label: '投诉' }
];
// 操作类气泡点完一项即关闭
function onPick(): void {
show.value = false;
}
</script>
<style lang="scss">
.popover-comment {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 12px 14px;
background-color: #f5f6f7;
border-radius: 10px;
}
.popover-comment-main {
flex: 1;
margin-right: 12px;
}
.popover-comment-name {
/* #ifndef APP */
display: block;
/* #endif */
font-size: 14px;
font-weight: bold;
color: #303133;
}
.popover-comment-text {
/* #ifndef APP */
display: block;
/* #endif */
font-size: 13px;
color: #606266;
margin-top: 4px;
}
.popover-more {
display: flex;
align-items: center;
justify-content: center;
padding: 6px;
}
/* 面板宽度由内容撑开:min-width 撑出五等分段,各操作项 flex: 1 均分 */
.popover-action-bar {
display: flex;
flex-direction: row;
align-items: center;
min-width: 300px;
padding: 16px 6px 14px 6px;
}
.popover-action-item {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
}
/* 深底白字:颜色必须落在 text 自身,App 端 text 不继承父级颜色 */
.popover-action-text {
/* #ifndef APP */
display: block;
/* #endif */
font-size: 13px;
color: #ffffff;
margin-top: 8px;
}
</style>遮罩与关闭行为
打开后试试点遮罩、点菜单项、追加若干菜单项再展开,观察面板是否重新贴合触发元素。
查看源码
<template>
<ux-cell-group>
<ux-cell-item title="半透明遮罩" :arrow="false">
<ux-switch v-model="dim"></ux-switch>
</ux-cell-item>
<ux-cell-item title="点遮罩关闭" :arrow="false">
<ux-switch v-model="maskCloseAble"></ux-switch>
</ux-cell-item>
<ux-cell-item title="点内容关闭" :arrow="false">
<ux-switch v-model="contentCloseAble"></ux-switch>
</ux-cell-item>
<ux-cell-item title="层级" :arrow="false" :border-bottom="false">
<view class="popover-segments">
<ux-button
v-for="(item, index) in zIndexList"
:key="index"
:type="zIndex == item.value ? 'primary' : 'default'"
custom-class="popover-segment"
size="small"
shape="round"
:text="item.label"
@click="zIndex = item.value"
></ux-button>
</view>
</ux-cell-item>
</ux-cell-group>
<!-- 主动作在前、次动作在后,两个动作统一小尺寸圆角按钮 -->
<ux-gap :height="12" />
<view class="popover-row">
<ux-popover
v-model:show="show"
:content-close-able="contentCloseAble"
:mask-bg-color="dim ? 'rgba(0,0,0,0.45)' : 'rgba(0,0,0,0)'"
:mask-close-able="maskCloseAble"
:refresh-key="refreshKey"
:z-index="zIndex"
placement="bottom-end"
>
<ux-button shape="round" size="small" type="primary" text="打开遮罩演示" @click="show = true"></ux-button>
<template #menu>
<view class="popover-menu">
<ux-cell-item title="编辑资料" :arrow="false" @click="onPick()"></ux-cell-item>
<ux-cell-item v-for="(item, index) in extraLabels" :key="index" :title="item.title" :arrow="false" :border-bottom="!item.last"></ux-cell-item>
</view>
</template>
</ux-popover>
<ux-button shape="round" size="small" text="追加菜单项" @click="addItem()"></ux-button>
</view>
</template>
<script setup lang="ts">
const show = ref<boolean>(false);
const dim = ref<boolean>(true);
const maskCloseAble = ref<boolean>(true);
const contentCloseAble = ref<boolean>(false);
// 留空取主题 dropdown 档,也能传具体数值
const zIndex = ref<string>('');
const refreshKey = ref<number>(0);
const extraItems = ref<number>(0);
const zIndexList: { label: string; value: string }[] = [
{ label: '默认', value: '' },
{ label: '2000', value: '2000' }
];
// 最后一项的分隔线由数据层给:模板里不读数组长度,否则原生端可能读到越界值
const extraLabels = computed<{ title: string; last: boolean }[]>((): { title: string; last: boolean }[] => {
const list: { title: string; last: boolean }[] = [];
for (let i = 0; i < extraItems.value; i++) {
list.push({ title: '新增菜单项 ' + (i + 1).toString(), last: i == extraItems.value - 1 });
}
return list;
});
// 菜单内容尺寸变了要改 refreshKey:组件不会自己重新测位,面板会停在旧位置
function addItem(): void {
extraItems.value = extraItems.value + 1;
refreshKey.value = refreshKey.value + 1;
}
// content-close-able 为 false 时组件不关,由调用方自己决定
function onPick(): void {
show.value = false;
}
</script>
<style lang="scss">
.popover-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
}
.popover-segments {
display: flex;
flex-direction: row;
align-items: center;
}
.popover-segment {
margin-left: 6px;
}
.popover-menu {
min-width: 180px;
}
</style>使用说明
接入前先确认这四条,能避开绝大多数「位置不对 / 关不掉 / 不跟随」的问题。
API
Props
| 属性 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
customStyle | 自定义面板容器样式 | string | JsonObject | - | - |
customClass | 自定义面板容器样式类 | string | '' | - |
placement | 面板方位 | PopoverPlacement | 'bottom-end' | - |
offset | 面板与触发元素的间距 | string | number | 8 | - |
showArrow | 是否显示箭头 | boolean | true | - |
arrowSize | 箭头边长 | string | number | 8 | - |
bgColor | 面板背景色,空值回退主题白色 | string | '' | - |
borderRadius | 面板圆角 | string | number | 8 | - |
showBorder | 是否显示面板描边:与 Ant Design / Element Plus 同口径,1px 描边定边界 | boolean | true | - |
showShadow | 是否显示漫射阴影 | boolean | true | - |
maskBgColor | 遮罩层背景色,默认全透明 | string | 'rgba(0,0,0,0)' | - |
maskCloseAble | 点击遮罩是否关闭 | boolean | true | - |
contentCloseAble | 点击面板内容是否关闭 | boolean | false | - |
duration | 显隐动画时长 | string | number | 200 | - |
zIndex | 层级,空值回退主题层级 | string | number | '' | - |
refreshKey | 内容尺寸变化的重测开关 | string | number | 0 | - |
Events
| 事件名 | 说明 | 回调参数 | 版本 |
|---|---|---|---|
open | 开始打开(面板已挂载、测位前) | - | - |
close | 开始关闭(终态已写回起始值) | - | - |
clickMask | 点击遮罩层 | - | - |
Slots
| 插槽名 | 说明 | 版本 |
|---|---|---|
default | 触发区内容(必填) | - |
menu | 面板内容 | - |
方法
通过 ref 获取 ux-popover 实例后调用:
| 方法名 | 说明 | 参数 | 返回值 |
|---|---|---|---|
open | 打开面板并重走一次测位流程,等价于把 v-model:show 置 true | - | - |
close | 播放退场动画并卸载面板,等价于把 v-model:show 置 false | - | - |
refresh | 重新量一次触发元素与面板的矩形并改写位置,不改变显隐状态 | - | - |
<template>
<ux-popover ref="popoverRef" v-model:show="show" placement="bottom-end">
<ux-button text="打开" @click="popoverRef?.open()"></ux-button>
<template #menu>
<view class="popover-menu">
<ux-cell-item title="刷新位置" :arrow="false" @click="popoverRef?.refresh()"></ux-cell-item>
</view>
</template>
</ux-popover>
</template>注意事项
- 面板只在打开时测位一次,不跟随滚动:Web 端打开期间发生任何滚动都会自动关闭(页面滚动、
scroll-view内滚动都算),因此不会错位;App 与小程序端不监听页面滚动,靠遮罩阻断触摸滑动来避免错位,如果代码主动调用scrollTo,面板会停在旧位置,需自行close()或refresh()。 - 触发区由组件用一层
view包裹并设align-self: flex-start,所以它能收缩到内容宽度;父容器不是 flex 时会退化成整行宽度,测位基准也随之变宽。把触发元素放进display: flex的容器里即可。 - 面板内容尺寸异步变化(图片加载、列表追加、条件渲染)不会自动重测,改
refreshKey值即可触发重测;也可以命令式调用refresh()。 contentCloseAble默认false:点菜单项组件不会自己关闭,需要在项目自身的点击回调里把v-model:show置false(否则面板会一直留在页面上)。maskBgColor默认为全透明,此时遮罩层不遮挡任何内容、只负责捕获点击;要让空白区域真正可点关闭,保留默认maskCloseAble即可,无需额外样式。- 面板默认 1px 描边 + 单层阴影:描边取主题边框色定边界,阴影
0 4px 12px只做浮层托底。App 端不保证渲染box-shadow,此时边界完全由描边承担,箭头也带描边,不会看不见。要纯色气泡(微信风格)就show-border="false"+show-shadow="false"配bg-color。 - 换成深色底后文字颜色不会自动反白:面板底色由组件给,文字颜色由
#menu插槽内容决定,记得给每个text单独设白字(App 端text不继承父级color)。 - 箭头绘制在面板之上(
z-index高于面板):它只描外露的两条斜边,伸进面板的部分由面板自身的底色盖住,不会在面板内圈出多余的线。 - 菜单里的
ux-button/ 任意块级元素要用block撑满时,把左右内边距交给外层容器(例如给包裹层加padding: 0 14px),不要在block元素上叠margin:block的width: 100%是相对内容宽,再叠外边距会把元素顶出面板右边缘,看起来像被截断。 - 越界只有两条规则:纵向先翻到另一侧,仍越界再钳制到 8px 留白;横向不翻转,只钳制左边界(面板比可用宽度还宽时把宽度压到视口宽 − 16)。所以贴着屏幕边缘的触发元素,面板会被往屏幕内侧推、箭头跟着偏移,不会翻到另一侧。另外面板是
position: fixed挂在组件原位,若某个祖先带transform/filter,它会成为面板的包含块——坐标以那个祖先为原点,还可能被祖先的overflow: hidden裁掉;这类容器里请改用弹出层方案(如ux-popup)。