Popover气泡卡片

点击/鼠标移入元素,弹出气泡式的卡片浮层。

何时使用

当目标元素有进一步的描述和相关操作时,可以收纳到卡片中,根据用户的操作行为进行展现。

Tooltip 的区别是,用户可以对浮层上的元素进行操作,因此它可以承载更复杂的内容,比如链接或按钮等。

单独引入此组件

想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

  1. import { NzPopoverModule } from 'ng-zorro-antd/popover';

代码演示

Popover气泡卡片 - 图1

基本

最简单的用法,浮层的大小由内容区域决定。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-popover-basic',
  4. template: `
  5. <button nz-button nz-popover nzType="primary" nzPopoverTitle="Title" nzPopoverContent="Content">
  6. Hover me
  7. </button>
  8. `
  9. })
  10. export class NzDemoPopoverBasicComponent {}

Popover气泡卡片 - 图2

从浮层内关闭

使用 nzVisible 属性控制浮层显示。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-popover-control',
  4. template: `
  5. <button
  6. nz-button
  7. nzType="primary"
  8. nz-popover
  9. nzPopoverTitle="Title"
  10. [(nzVisible)]="visible"
  11. (nzVisibleChange)="change($event)"
  12. nzPopoverTrigger="click"
  13. [nzPopoverContent]="contentTemplate"
  14. >
  15. Click me
  16. </button>
  17. <ng-template #contentTemplate>
  18. <a (click)="clickMe()">Close</a>
  19. </ng-template>
  20. `
  21. })
  22. export class NzDemoPopoverControlComponent {
  23. visible: boolean;
  24. clickMe(): void {
  25. this.visible = false;
  26. }
  27. change(value: boolean): void {
  28. console.log(value);
  29. }
  30. }

Popover气泡卡片 - 图3

模板渲染

nzContentnzTitle 可以传入 TemplateRef<void> 模板渲染。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-popover-template',
  4. template: `
  5. <button nz-button nz-popover [nzPopoverTitle]="titleTemplate" [nzPopoverContent]="contentTemplate">
  6. Render Template
  7. </button>
  8. <ng-template #titleTemplate><i nz-icon nzType="close"></i> Title</ng-template>
  9. <ng-template #contentTemplate><i nz-icon nzType="check"></i> Content</ng-template>
  10. `
  11. })
  12. export class NzDemoPopoverTemplateComponent {}

Popover气泡卡片 - 图4

位置

位置有十二个方向。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-popover-placement',
  4. template: `
  5. <div style="margin-left: 60px">
  6. <button
  7. nz-button
  8. nz-popover
  9. nzPopoverTitle="Title"
  10. [nzPopoverContent]="contentTemplate"
  11. nzPopoverPlacement="topLeft"
  12. >
  13. TL
  14. </button>
  15. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="top">
  16. Top
  17. </button>
  18. <button
  19. nz-button
  20. nz-popover
  21. nzPopoverTitle="Title"
  22. [nzPopoverContent]="contentTemplate"
  23. nzPopoverPlacement="topRight"
  24. >
  25. TR
  26. </button>
  27. </div>
  28. <div style="width: 60px; float: left;">
  29. <button
  30. nz-button
  31. nz-popover
  32. nzPopoverTitle="Title"
  33. [nzPopoverContent]="contentTemplate"
  34. nzPopoverPlacement="leftTop"
  35. >
  36. LT
  37. </button>
  38. <button
  39. nz-button
  40. nz-popover
  41. nzPopoverTitle="Title"
  42. [nzPopoverContent]="contentTemplate"
  43. nzPopoverPlacement="left"
  44. >
  45. Left
  46. </button>
  47. <button
  48. nz-button
  49. nz-popover
  50. nzPopoverTitle="Title"
  51. [nzPopoverContent]="contentTemplate"
  52. nzPopoverPlacement="leftBottom"
  53. >
  54. LB
  55. </button>
  56. </div>
  57. <div style="width: 60px; margin-left: 252px;">
  58. <button
  59. nz-button
  60. nz-popover
  61. nzPopoverTitle="Title"
  62. [nzPopoverContent]="contentTemplate"
  63. nzPopoverPlacement="rightTop"
  64. >
  65. RT
  66. </button>
  67. <button
  68. nz-button
  69. nz-popover
  70. nzPopoverTitle="Title"
  71. [nzPopoverContent]="contentTemplate"
  72. nzPopoverPlacement="right"
  73. >
  74. Right
  75. </button>
  76. <button
  77. nz-button
  78. nz-popover
  79. nzPopoverTitle="Title"
  80. [nzPopoverContent]="contentTemplate"
  81. nzPopoverPlacement="rightBottom"
  82. >
  83. RB
  84. </button>
  85. </div>
  86. <div style="margin-left: 60px; clear: both;">
  87. <button
  88. nz-button
  89. nz-popover
  90. nzPopoverTitle="Title"
  91. [nzPopoverContent]="contentTemplate"
  92. nzPopoverPlacement="bottomLeft"
  93. >
  94. BL
  95. </button>
  96. <button
  97. nz-button
  98. nz-popover
  99. nzPopoverTitle="Title"
  100. [nzPopoverContent]="contentTemplate"
  101. nzPopoverPlacement="bottom"
  102. >
  103. Bottom
  104. </button>
  105. <button
  106. nz-button
  107. nz-popover
  108. nzPopoverTitle="Title"
  109. [nzPopoverContent]="contentTemplate"
  110. nzPopoverPlacement="bottomRight"
  111. >
  112. BR
  113. </button>
  114. </div>
  115. <ng-template #contentTemplate>
  116. <div>
  117. <p>Content</p>
  118. <p>Content</p>
  119. </div>
  120. </ng-template>
  121. `,
  122. styles: [
  123. `
  124. button {
  125. margin-right: 8px;
  126. margin-bottom: 8px;
  127. width: 70px;
  128. text-align: center;
  129. padding: 0;
  130. }
  131. `
  132. ]
  133. })
  134. export class NzDemoPopoverPlacementComponent {}

Popover气泡卡片 - 图5

箭头指向

通过设置 nzPlacement ,可以箭头将指向目标元素的中心。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-popover-arrow-point-at-center',
  4. template: `
  5. <button nz-button nzPopoverTitle="Title" nzPopoverContent="Content" nzPopoverPlacement="topLeft" nz-popover>
  6. Align edge / 边缘对齐
  7. </button>
  8. <button nz-button nzPopoverTitle="Title" nzPopoverContent="Content" nzPopoverPlacement="topCenter" nz-popover>
  9. Arrow points to center / 箭头指向中心
  10. </button>
  11. `,
  12. styles: [
  13. `
  14. button {
  15. margin-right: 8px;
  16. margin-bottom: 8px;
  17. }
  18. `
  19. ]
  20. })
  21. export class NzDemoPopoverArrowPointAtCenterComponent {}

Popover气泡卡片 - 图6

三种触发方式

鼠标移入、聚集、点击。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-popover-triggerType',
  4. template: `
  5. <ng-template #contentTemplate>
  6. <div>
  7. <p>Content</p>
  8. <p>Content</p>
  9. </div>
  10. </ng-template>
  11. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverTrigger="click">
  12. Click me
  13. </button>
  14. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverTrigger="hover">
  15. Hover me
  16. </button>
  17. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverTrigger="focus">
  18. Focus me
  19. </button>
  20. `,
  21. styles: [
  22. `
  23. button {
  24. margin-right: 8px;
  25. }
  26. `
  27. ]
  28. })
  29. export class NzDemoPopoverTriggerTypeComponent {}

API

[nz-popover]directive

参数说明类型默认值
[nzPopoverTitle]标题string | TemplateRef<void>-
[nzPopoverContent]用于定义内容string | TemplateRef<void>-
[nzPopoverTrigger]触发行为'click' | 'focus' | 'hover''hover'
[nzPopoverPlacement]气泡框位置'top' | 'left' | 'right' | 'bottom' | 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight' | 'leftTop' | 'leftBottom' | 'rightTop' | 'rightBottom''top'

更多属性请参考 Tooltip

注意

请确保 nz-popover 的子元素能接受 onMouseEnteronMouseLeaveonFocusonClick 事件。