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. <nz-space nzSize="middle">
  13. <nz-space-item>
  14. <nz-skeleton-element
  15. nzType="button"
  16. [nzActive]="elementActive"
  17. [nzSize]="elementSize"
  18. [nzShape]="buttonShape"
  19. ></nz-skeleton-element>
  20. </nz-space-item>
  21. <nz-space-item>
  22. <nz-skeleton-element
  23. nzType="avatar"
  24. [nzActive]="elementActive"
  25. [nzSize]="elementSize"
  26. [nzShape]="avatarShape"
  27. ></nz-skeleton-element>
  28. </nz-space-item>
  29. <nz-space-item>
  30. <nz-skeleton-element nzType="input" [nzActive]="elementActive" [nzSize]="elementSize" style="width:200px"></nz-skeleton-element>
  31. </nz-space-item>
  32. </nz-space>
  33. <br />
  34. <br />
  35. <nz-skeleton-element nzType="image" [nzActive]="elementActive"></nz-skeleton-element>
  36. <nz-divider></nz-divider>
  37. <div nz-row nzAlign="middle" [nzGutter]="8">
  38. <div nz-col nzSpan="10">
  39. Size:
  40. <nz-radio-group [(ngModel)]="elementSize">
  41. <label nz-radio-button nzValue="default">Default</label>
  42. <label nz-radio-button nzValue="large">Large</label>
  43. <label nz-radio-button nzValue="small">Small</label>
  44. </nz-radio-group>
  45. </div>
  46. <div nz-col nzSpan="5">
  47. Active:
  48. <nz-switch [(ngModel)]="elementActive"></nz-switch>
  49. </div>
  50. </div>
  51. <br />
  52. <br />
  53. <div nz-row nzAlign="middle" [nzGutter]="8">
  54. <div nz-col nzSpan="10">
  55. Button Shape:
  56. <nz-radio-group [(ngModel)]="buttonShape">
  57. <label nz-radio-button nzValue="default">Default</label>
  58. <label nz-radio-button nzValue="circle">Circle</label>
  59. <label nz-radio-button nzValue="round">Round</label>
  60. </nz-radio-group>
  61. </div>
  62. <div nz-col nzSpan="10">
  63. Avatar Shape:
  64. <nz-radio-group [(ngModel)]="avatarShape">
  65. <label nz-radio-button nzValue="circle">Circle</label>
  66. <label nz-radio-button nzValue="square">Square</label>
  67. </nz-radio-group>
  68. </div>
  69. </div>
  70. `
  71. })
  72. export class NzDemoSkeletonElementComponent {
  73. buttonActive = false;
  74. avatarActive = false;
  75. inputActive = false;
  76. imageActive = false;
  77. buttonSize: NzSkeletonButtonSize = 'default';
  78. avatarSize: NzSkeletonAvatarSize = 'default';
  79. inputSize: NzSkeletonInputSize = 'default';
  80. elementActive = false;
  81. buttonShape: NzSkeletonButtonShape = 'default';
  82. avatarShape: NzSkeletonAvatarShape = 'circle';
  83. elementSize: NzSkeletonInputSize = 'default';
  84. }

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
[nzRound]为 true 时,段落和标题显示圆角booleanfalse

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’

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

属性说明类型默认值
[nzActive]是否展示动画效果booleanfalse