Popover气泡卡片

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

何时使用

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

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

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

代码演示Popover气泡卡片 - 图2

Popover气泡卡片 - 图3

基本

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

  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气泡卡片 - 图4

从浮层内关闭

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

  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. [(nzPopoverVisible)]="visible"
  11. (nzPopoverVisibleChange)="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 = false;
  24. clickMe(): void {
  25. this.visible = false;
  26. }
  27. change(value: boolean): void {
  28. console.log(value);
  29. }
  30. }

Popover气泡卡片 - 图5

模板渲染

nzPopoverContentnzPopoverTitle 可以传入 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气泡卡片 - 图6

位置

位置有十二个方向。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-popover-placement',
  4. template: `
  5. <div style="margin-left: 60px">
  6. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="topLeft">
  7. TL
  8. </button>
  9. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="top">
  10. Top
  11. </button>
  12. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="topRight">
  13. TR
  14. </button>
  15. </div>
  16. <div style="width: 60px; float: left;">
  17. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="leftTop">
  18. LT
  19. </button>
  20. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="left">
  21. Left
  22. </button>
  23. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="leftBottom">
  24. LB
  25. </button>
  26. </div>
  27. <div style="width: 60px; margin-left: 252px;">
  28. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="rightTop">
  29. RT
  30. </button>
  31. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="right">
  32. Right
  33. </button>
  34. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="rightBottom">
  35. RB
  36. </button>
  37. </div>
  38. <div style="margin-left: 60px; clear: both;">
  39. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="bottomLeft">
  40. BL
  41. </button>
  42. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="bottom">
  43. Bottom
  44. </button>
  45. <button nz-button nz-popover nzPopoverTitle="Title" [nzPopoverContent]="contentTemplate" nzPopoverPlacement="bottomRight">
  46. BR
  47. </button>
  48. </div>
  49. <ng-template #contentTemplate>
  50. <div>
  51. <p>Content</p>
  52. <p>Content</p>
  53. </div>
  54. </ng-template>
  55. `,
  56. styles: [
  57. `
  58. button {
  59. margin-right: 8px;
  60. margin-bottom: 8px;
  61. width: 70px;
  62. text-align: center;
  63. padding: 0;
  64. }
  65. `
  66. ]
  67. })
  68. export class NzDemoPopoverPlacementComponent {}

Popover气泡卡片 - 图7

箭头指向

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

  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气泡卡片 - 图8

三种触发方式

鼠标移入、聚集、点击。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-popover-trigger-type',
  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]触发行为,为 null 时不响应光标事件‘click’ | ‘focus’ | ‘hover’ | null‘hover’
[nzPopoverPlacement]气泡框位置‘top’ | ‘left’ | ‘right’ | ‘bottom’ | ‘topLeft’ | ‘topRight’ | ‘bottomLeft’ | ‘bottomRight’ | ‘leftTop’ | ‘leftBottom’ | ‘rightTop’ | ‘rightBottom’‘top’
[nzPopoverOrigin]气泡框定位元素ElementRef-
[nzPopoverVisible]显示隐藏气泡框booleanfalse
(nzPopoverVisibleChange)显示隐藏的事件EventEmitter<boolean>-
[nzPopoverMouseEnterDelay]鼠标移入后延时多少才显示气泡框,单位:秒number0.15
[nzPopoverMouseLeaveDelay]鼠标移出后延时多少才隐藏气泡框,单位:秒number0.1
[nzPopoverOverlayClassName]卡片类名string-
[nzPopoverOverlayStyle]卡片样式object-

更多属性请参考 Tooltip

注意

请确保 [nz-popover] 元素能接受 onMouseEnteronMouseLeaveonFocusonClick 事件。