Skeleton骨架屏

在需要等待加载内容的位置提供一个占位图形组合。

何时使用

  • 网络较慢,需要长时间等待加载处理的情况下。
  • 图文信息内容较多的列表/卡片中。
  • 只适合用在第一次加载数据的场景。
  • 可以被 Spin 完全代替,但是在可用的场景下可以比 Spin 提供更好的视觉效果和用户体验
  1. import { NzSkeletonModule } from 'ng-zorro-antd/skeleton';

代码演示Skeleton骨架屏 - 图2

Skeleton骨架屏 - 图3

基本

最简单的用法。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-skeleton-basic',
  4. template: `
  5. <nz-skeleton></nz-skeleton>
  6. `
  7. })
  8. export class NzDemoSkeletonBasicComponent {}

Skeleton骨架屏 - 图4

复杂的组合

更复杂的组合。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-skeleton-complex',
  4. template: `
  5. <nz-skeleton [nzAvatar]="true" [nzParagraph]="{ rows: 4 }"></nz-skeleton>
  6. `
  7. })
  8. export class NzDemoSkeletonComplexComponent {}

Skeleton骨架屏 - 图5

动画效果

显示动画效果。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-skeleton-active',
  4. template: `
  5. <nz-skeleton [nzActive]="true"></nz-skeleton>
  6. `
  7. })
  8. export class NzDemoSkeletonActiveComponent {}

Skeleton骨架屏 - 图6

骨架按钮、头像和输入框。

骨架按钮、头像和输入框。

  1. import { Component } from '@angular/core';
  2. import {
  3. NzSkeletonAvatarShape,
  4. NzSkeletonAvatarSize,
  5. NzSkeletonButtonShape,
  6. NzSkeletonButtonSize,
  7. NzSkeletonInputSize
  8. } from 'ng-zorro-antd/skeleton';
  9. @Component({
  10. selector: 'nz-demo-skeleton-element',
  11. template: `
  12. <div nz-row nzAlign="middle" [nzGutter]="8">
  13. <div nz-col nzSpan="5">
  14. ButtonActive:
  15. <nz-switch [(ngModel)]="buttonActive"></nz-switch>
  16. </div>
  17. <div nz-col nzSpan="9">
  18. ButtonSize:
  19. <nz-radio-group [(ngModel)]="buttonSize">
  20. <label nz-radio-button nzValue="default">Default</label>
  21. <label nz-radio-button nzValue="large">Large</label>
  22. <label nz-radio-button nzValue="small">Small</label>
  23. </nz-radio-group>
  24. </div>
  25. <div nz-col nzSpan="9">
  26. ButtonShape:
  27. <nz-radio-group [(ngModel)]="buttonShape">
  28. <label nz-radio-button nzValue="default">Default</label>
  29. <label nz-radio-button nzValue="circle">Circle</label>
  30. <label nz-radio-button nzValue="round">Round</label>
  31. </nz-radio-group>
  32. </div>
  33. </div>
  34. <br />
  35. <nz-skeleton-element nzType="button" [nzActive]="buttonActive" [nzSize]="buttonSize" [nzShape]="buttonShape"></nz-skeleton-element>
  36. <br />
  37. <br />
  38. <div nz-row nzAlign="middle" [nzGutter]="8">
  39. <div nz-col nzSpan="5">
  40. AvatarActive:
  41. <nz-switch [(ngModel)]="avatarActive"></nz-switch>
  42. </div>
  43. <div nz-col nzSpan="9">
  44. AvatarSize:
  45. <nz-radio-group [(ngModel)]="avatarSize">
  46. <label nz-radio-button nzValue="default">Default</label>
  47. <label nz-radio-button nzValue="large">Large</label>
  48. <label nz-radio-button nzValue="small">Small</label>
  49. </nz-radio-group>
  50. </div>
  51. <div nz-col nzSpan="9">
  52. AvatarShape:
  53. <nz-radio-group [(ngModel)]="avatarShape">
  54. <label nz-radio-button nzValue="circle">Circle</label>
  55. <label nz-radio-button nzValue="square">Square</label>
  56. </nz-radio-group>
  57. </div>
  58. </div>
  59. <br />
  60. <nz-skeleton-element nzType="avatar" [nzActive]="avatarActive" [nzSize]="avatarSize" [nzShape]="avatarShape"></nz-skeleton-element>
  61. <br />
  62. <br />
  63. <div nz-row nzAlign="middle" [nzGutter]="8">
  64. <div nz-col nzSpan="5">
  65. InputActive:
  66. <nz-switch [(ngModel)]="inputActive"></nz-switch>
  67. </div>
  68. <div nz-col nzSpan="9">
  69. InputSize:
  70. <nz-radio-group [(ngModel)]="inputSize">
  71. <label nz-radio-button nzValue="default">Default</label>
  72. <label nz-radio-button nzValue="large">Large</label>
  73. <label nz-radio-button nzValue="small">Small</label>
  74. </nz-radio-group>
  75. </div>
  76. </div>
  77. <br />
  78. <nz-skeleton-element nzType="input" [nzActive]="inputActive" [nzSize]="inputSize" style="width:300px"></nz-skeleton-element>
  79. `
  80. })
  81. export class NzDemoSkeletonElementComponent {
  82. buttonActive = false;
  83. avatarActive = false;
  84. inputActive = false;
  85. buttonSize: NzSkeletonButtonSize = 'default';
  86. avatarSize: NzSkeletonAvatarSize = 'default';
  87. inputSize: NzSkeletonInputSize = 'default';
  88. buttonShape: NzSkeletonButtonShape = 'default';
  89. avatarShape: NzSkeletonAvatarShape = 'circle';
  90. }

Skeleton骨架屏 - 图7

包含子组件

加载占位图包含子组件。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-skeleton-children',
  4. template: `
  5. <div class="article">
  6. <nz-skeleton [nzLoading]="loading">
  7. <h4>Ant Design, a design language</h4>
  8. <p>
  9. We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people
  10. create their product prototypes beautifully and efficiently.
  11. </p>
  12. </nz-skeleton>
  13. <button nz-button (click)="showSkeleton()" [disabled]="loading">
  14. Show Skeleton
  15. </button>
  16. </div>
  17. `,
  18. styles: [
  19. `
  20. .article h4 {
  21. margin-bottom: 16px;
  22. }
  23. .article button {
  24. margin-top: 16px;
  25. }
  26. `
  27. ]
  28. })
  29. export class NzDemoSkeletonChildrenComponent {
  30. loading = false;
  31. showSkeleton(): void {
  32. this.loading = true;
  33. setTimeout(() => {
  34. this.loading = false;
  35. }, 3000);
  36. }
  37. }

Skeleton骨架屏 - 图8

列表样例

在列表组件中使用加载占位符。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-skeleton-list',
  4. template: `
  5. <nz-switch [(ngModel)]="loading"></nz-switch>
  6. <nz-list [nzDataSource]="listData" [nzRenderItem]="item" [nzItemLayout]="'vertical'">
  7. <ng-template #item let-item>
  8. <nz-list-item
  9. [nzContent]="loading ? ' ' : item.content"
  10. [nzActions]="loading ? [] : [starAction, likeAction, msgAction]"
  11. [nzExtra]="loading ? null : extra"
  12. >
  13. <nz-skeleton [nzLoading]="loading" [nzActive]="true" [nzAvatar]="true">
  14. <ng-template #starAction><i nz-icon nzType="star-o" style="margin-right: 8px;"></i> 156</ng-template>
  15. <ng-template #likeAction><i nz-icon nzType="like-o" style="margin-right: 8px;"></i> 156</ng-template>
  16. <ng-template #msgAction><i nz-icon nzType="message" style="margin-right: 8px;"></i> 2</ng-template>
  17. <nz-list-item-meta [nzAvatar]="item.avatar" [nzTitle]="nzTitle" [nzDescription]="item.description">
  18. <ng-template #nzTitle
  19. ><a href="{{ item.href }}">{{ item.title }}</a></ng-template
  20. >
  21. </nz-list-item-meta>
  22. <ng-template #extra>
  23. <img width="272" alt="logo" src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png" />
  24. </ng-template>
  25. </nz-skeleton>
  26. </nz-list-item>
  27. </ng-template>
  28. </nz-list>
  29. `
  30. })
  31. export class NzDemoSkeletonListComponent {
  32. loading = true;
  33. listData = new Array(3).fill({}).map((_i, index) => {
  34. return {
  35. href: 'http://ng.ant.design',
  36. title: `ant design part ${index}`,
  37. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  38. description: 'Ant Design, a design language for background applications, is refined by Ant UED Team.',
  39. content:
  40. 'We supply a series of design principles, practical patterns and high quality design resources ' +
  41. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.'
  42. };
  43. });
  44. }

API

nz-skeletoncomponent

属性说明类型默认值
[nzActive]是否展示动画效果booleanfalse
[nzAvatar]是否显示头像占位图boolean | NzSkeletonAvatarfalse
[nzLoading]true 时,显示占位图。反之则直接展示子组件boolean-
[nzParagraph]是否显示段落占位图boolean | NzSkeletonParagraphtrue
[nzTitle]是否显示标题占位图boolean | NzSkeletonTitletrue

NzSkeletonAvatar

属性说明类型默认值
size设置头像占位图的大小number | ‘large’ | ‘small’ | ‘default’-
shape指定头像的形状‘circle’ | ‘square’-

NzSkeletonTitle

属性说明类型默认值
width设置标题占位图的宽度number | string-

NzSkeletonParagraph

属性说明类型默认值
rows设置段落占位图的行数number-
width设置标题占位图的宽度,若为数组时则为对应的每行宽度,反之则是最后一行的宽度number | string | Array<number | string>-

nz-skeleton-element [nzType=”button”]component

属性说明类型默认值
[nzActive]是否展示动画效果booleanfalse
[nzSize]大小‘large’ | ‘small’ | ‘default’‘default’
[nzShape]形状‘circle’ | ‘round’ | ‘default’‘default’

nz-skeleton-element [nzType=”avatar”]component

属性说明类型默认值
[nzActive]是否展示动画效果booleanfalse
[nzSize]大小number | ‘large’ | ‘small’ | ‘default’‘default’
[nzShape]形状‘circle’ | ‘square’‘square’

nz-skeleton-element [nzType=”input”]component

属性说明类型默认值
[nzActive]是否展示动画效果booleanfalse
[nzSize]大小‘large’ | ‘small’ | ‘default’‘default’