Popover 气泡卡片

基础用法

与Tooltip相似,Popover也是基于ElPopper的构建。 因此对于重复属性,请参考 Tooltip 的文档,在此文档中不做详尽解释。

trigger 属性被用来决定 popover 的触发方式,支持的触发方式: hoverclickfocuscontextmenu。 如果你想手动控制它,你可以设置 v-model:visible

Popover 弹出框 - 图1

  1. <template>
  2. <el-popover
  3. placement="top-start"
  4. title="Title"
  5. :width="200"
  6. trigger="hover"
  7. content="this is content, this is content, this is content"
  8. >
  9. <template #reference>
  10. <el-button>Hover to activate</el-button>
  11. </template>
  12. </el-popover>
  13. <el-popover
  14. placement="bottom"
  15. title="Title"
  16. :width="200"
  17. trigger="click"
  18. content="this is content, this is content, this is content"
  19. >
  20. <template #reference>
  21. <el-button>Click to activate</el-button>
  22. </template>
  23. </el-popover>
  24. <el-popover
  25. ref="popover"
  26. placement="right"
  27. title="Title"
  28. :width="200"
  29. trigger="focus"
  30. content="this is content, this is content, this is content"
  31. >
  32. <template #reference>
  33. <el-button>Focus to activate</el-button>
  34. </template>
  35. </el-popover>
  36. <el-popover
  37. ref="popover"
  38. title="Title"
  39. :width="200"
  40. trigger="contextmenu"
  41. content="this is content, this is content, this is content"
  42. >
  43. <template #reference>
  44. <el-button>contextmenu to activate</el-button>
  45. </template>
  46. </el-popover>
  47. <el-popover
  48. :visible="visible"
  49. placement="bottom"
  50. title="Title"
  51. :width="200"
  52. content="this is content, this is content, this is content"
  53. >
  54. <template #reference>
  55. <el-button @click="visible = !visible">Manual to activate</el-button>
  56. </template>
  57. </el-popover>
  58. </template>
  59. <script lang="ts" setup>
  60. import { ref } from 'vue'
  61. const visible = ref(false)
  62. </script>

虚拟触发

像 Tooltip一样,Popover 可以由虚拟元素触发,这个功能就很适合使用在触发元素和展示内容元素是分开的场景。通常我们使用 #reference 来放置我们的触发元素, 用 triggering-element API,您可以任意设置您的触发元素 但注意到触发元素应该是接受 mousekeyboard 事件的元素。

WARNING

v-popover 将被废弃,请使用 virtual-ref 作为替代。

Popover 弹出框 - 图2

  1. <template>
  2. <el-button ref="buttonRef" v-click-outside="onClickOutside"
  3. >Click me</el-button
  4. >
  5. <el-popover
  6. ref="popoverRef"
  7. :virtual-ref="buttonRef"
  8. trigger="click"
  9. title="With title"
  10. virtual-triggering
  11. >
  12. <span> Some content </span>
  13. </el-popover>
  14. </template>
  15. <script setup lang="ts">
  16. import { ref, unref } from 'vue'
  17. import { ClickOutside as vClickOutside } from 'element-plus'
  18. const buttonRef = ref()
  19. const popoverRef = ref()
  20. const onClickOutside = () => {
  21. unref(popoverRef).popperRef?.delayHide?.()
  22. }
  23. </script>

内容可扩展

可以在 Popover 中嵌套其它组件, 以下为嵌套表格的例子。

利用插槽取代 content 属性

Popover 弹出框 - 图3

  1. <template>
  2. <div style="display: flex; align-items: center">
  3. <el-popover placement="right" :width="400" trigger="click">
  4. <template #reference>
  5. <el-button style="margin-right: 16px">Click to activate</el-button>
  6. </template>
  7. <el-table :data="gridData">
  8. <el-table-column width="150" property="date" label="date" />
  9. <el-table-column width="100" property="name" label="name" />
  10. <el-table-column width="300" property="address" label="address" />
  11. </el-table>
  12. </el-popover>
  13. <el-popover
  14. :width="300"
  15. popper-style="box-shadow: rgb(14 18 22 / 35%) 0px 10px 38px -10px, rgb(14 18 22 / 20%) 0px 10px 20px -15px; padding: 20px;"
  16. >
  17. <template #reference>
  18. <el-avatar src="https://avatars.githubusercontent.com/u/72015883?v=4" />
  19. </template>
  20. <template #default>
  21. <div
  22. class="demo-rich-conent"
  23. style="display: flex; gap: 16px; flex-direction: column"
  24. >
  25. <el-avatar
  26. :size="60"
  27. src="https://avatars.githubusercontent.com/u/72015883?v=4"
  28. style="margin-bottom: 8px"
  29. />
  30. <div>
  31. <p
  32. class="demo-rich-content__name"
  33. style="margin: 0; font-weight: 500"
  34. >
  35. Element Plus
  36. </p>
  37. <p
  38. class="demo-rich-content__mention"
  39. style="margin: 0; font-size: 14px; color: var(--el-color-info)"
  40. >
  41. @element-plus
  42. </p>
  43. </div>
  44. <p class="demo-rich-content__desc" style="margin: 0">
  45. Element Plus, a Vue 3 based component library for developers,
  46. designers and product managers
  47. </p>
  48. </div>
  49. </template>
  50. </el-popover>
  51. </div>
  52. </template>
  53. <script lang="ts" setup>
  54. const gridData = [
  55. {
  56. date: '2016-05-02',
  57. name: 'Jack',
  58. address: 'New York City',
  59. },
  60. {
  61. date: '2016-05-04',
  62. name: 'Jack',
  63. address: 'New York City',
  64. },
  65. {
  66. date: '2016-05-01',
  67. name: 'Jack',
  68. address: 'New York City',
  69. },
  70. {
  71. date: '2016-05-03',
  72. name: 'Jack',
  73. address: 'New York City',
  74. },
  75. ]
  76. </script>

嵌套操作

当然,你还可以嵌套操作, 它比使用dialog更加轻量。

Popover 弹出框 - 图4

  1. <template>
  2. <el-popover v-model:visible="visible" placement="top" :width="160">
  3. <p>Are you sure to delete this?</p>
  4. <div style="text-align: right; margin: 0">
  5. <el-button size="small" text @click="visible = false">cancel</el-button>
  6. <el-button size="small" type="primary" @click="visible = false"
  7. >confirm</el-button
  8. >
  9. </div>
  10. <template #reference>
  11. <el-button @click="visible = true">Delete</el-button>
  12. </template>
  13. </el-popover>
  14. </template>
  15. <script lang="ts" setup>
  16. import { ref } from 'vue'
  17. const visible = ref(false)
  18. </script>

指令

您可以使用指令性方式弹出窗口,但这种方法不再推荐 ,因为这使得应用程序变得复杂, 您可以参考虚拟触发来实现一样的效果。

Popover 弹出框 - 图5

  1. <template>
  2. <el-button v-popover="popoverRef" v-click-outside="onClickOutside"
  3. >Click me</el-button
  4. >
  5. <el-popover
  6. ref="popoverRef"
  7. trigger="click"
  8. title="With title"
  9. virtual-triggering
  10. persistent
  11. >
  12. <span> Some content </span>
  13. </el-popover>
  14. </template>
  15. <script setup lang="ts">
  16. import { ref, unref } from 'vue'
  17. import { ClickOutside as vClickOutside } from 'element-plus'
  18. const buttonRef = ref()
  19. const popoverRef = ref()
  20. const onClickOutside = () => {
  21. unref(popoverRef).popperRef?.delayHide?.()
  22. }
  23. </script>

Popover 属性

属性说明类型可选值默认值
trigger触发方式stringclick/focus/hover/contextmenuclick
title标题string
effectTooltip 主题,Element Plus 内置了 dark / light 两种主题stringstringlight
content显示的内容,也可以通过写入默认 slot 修改显示内容string
width宽度string / number最小宽度 150px
placement出现位置stringtop/top-start/top-end/bottom/bottom-start/bottom-end/left/left-start/left-end/right/right-start/right-endbottom
disabledPopover 是否可用booleanfalse
visible / v-model:visiblePopover 是否显示Booleanfalse
offset出现位置的偏移量number0
transition定义渐变动画stringel-fade-in-linear
show-arrow是否显示 Tooltip 箭头, 欲了解更多信息,请参考 ElPopper Popover 弹出框 - 图6booleantrue
popper-optionspopper.js Popover 弹出框 - 图7 的参数object详情参考 popper.js Popover 弹出框 - 图8{ boundariesElement: ‘body’, gpuAcceleration: false }
popper-class为 popper 添加类名string
show-after延迟出现,单位毫秒number0
hide-after延迟关闭,单位毫秒number200
auto-closeTooltip 出现后自动隐藏延时,单位毫秒,为 0 则不会自动隐藏number0
tabindexPopover 组件的 tabindex Popover 弹出框 - 图9number
teleported是否将 popover 的下拉列表插入至 body 元素booleantrue / falsetrue
persistent当 popover 组件长时间不触发且 persistent 属性设置为 false 时, popover 将会被删除booleantrue

Popover 插槽

插槽名说明
Popover 内嵌 HTML 文本
reference触发 Popover 显示的 HTML 元素

Popover 事件

事件名说明回调参数
show显示时触发
before-enter显示动画播放前触发
after-enter显示动画播放完毕后触发
hide隐藏时触发
before-leave隐藏动画播放前触发
after-leave隐藏动画播放完毕后触发

源代码

组件 Popover 弹出框 - 图10 文档 Popover 弹出框 - 图11

贡献者

Popover 弹出框 - 图12 三咲智子

Popover 弹出框 - 图13 JeremyWuuuuu

Popover 弹出框 - 图14 云游君

Popover 弹出框 - 图15 啝裳

Popover 弹出框 - 图16 btea

Popover 弹出框 - 图17 zz

Popover 弹出框 - 图18 Xc

Popover 弹出框 - 图19 msidolphin

Popover 弹出框 - 图20 Alan Wang

Popover 弹出框 - 图21 C.Y.Kun

Popover 弹出框 - 图22 kooriookami

Popover 弹出框 - 图23 Delyan Haralanov

Popover 弹出框 - 图24 bqy

Popover 弹出框 - 图25 BigPengZai

Popover 弹出框 - 图26 Aex

Popover 弹出框 - 图27 zouhang

Popover 弹出框 - 图28 on the field of hope

Popover 弹出框 - 图29 Hades-li

Popover 弹出框 - 图30 Ghislain B