Skeleton骨架屏
在需要等待加载内容的位置提供一个占位图形组合。
何时使用
- 网络较慢,需要长时间等待加载处理的情况下。
- 图文信息内容较多的列表/卡片中。
- 只适合用在第一次加载数据的场景。
- 可以被 Spin 完全代替,但是在可用的场景下可以比 Spin 提供更好的视觉效果和用户体验
import { NzSkeletonModule } from 'ng-zorro-antd/skeleton';
代码演示
基本
最简单的用法。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-skeleton-basic',
template: `
<nz-skeleton></nz-skeleton>
`
})
export class NzDemoSkeletonBasicComponent {}
复杂的组合
更复杂的组合。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-skeleton-complex',
template: `
<nz-skeleton [nzAvatar]="true" [nzParagraph]="{ rows: 4 }"></nz-skeleton>
`
})
export class NzDemoSkeletonComplexComponent {}
动画效果
显示动画效果。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-skeleton-active',
template: `
<nz-skeleton [nzActive]="true"></nz-skeleton>
`
})
export class NzDemoSkeletonActiveComponent {}
骨架按钮、头像和输入框。
骨架按钮、头像和输入框。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-skeleton-element',
template: `
<div nz-row nzAlign="middle" [nzGutter]="8">
<div nz-col nzSpan="5">
ButtonActive:
<nz-switch [(ngModel)]="buttonActive"></nz-switch>
</div>
<div nz-col nzSpan="9">
ButtonSize:
<nz-radio-group [(ngModel)]="buttonSize">
<label nz-radio-button nzValue="default">Default</label>
<label nz-radio-button nzValue="large">Large</label>
<label nz-radio-button nzValue="small">Small</label>
</nz-radio-group>
</div>
<div nz-col nzSpan="9">
ButtonShape:
<nz-radio-group [(ngModel)]="buttonShape">
<label nz-radio-button nzValue="default">Default</label>
<label nz-radio-button nzValue="circle">Circle</label>
<label nz-radio-button nzValue="round">Round</label>
</nz-radio-group>
</div>
</div>
<br />
<nz-skeleton-element nzType="button" [nzActive]="buttonActive" [nzSize]="buttonSize" [nzShape]="buttonShape"></nz-skeleton-element>
<br />
<br />
<div nz-row nzAlign="middle" [nzGutter]="8">
<div nz-col nzSpan="5">
AvatarActive:
<nz-switch [(ngModel)]="avatarActive"></nz-switch>
</div>
<div nz-col nzSpan="9">
AvatarSize:
<nz-radio-group [(ngModel)]="avatarSize">
<label nz-radio-button nzValue="default">Default</label>
<label nz-radio-button nzValue="large">Large</label>
<label nz-radio-button nzValue="small">Small</label>
</nz-radio-group>
</div>
<div nz-col nzSpan="9">
AvatarShape:
<nz-radio-group [(ngModel)]="avatarShape">
<label nz-radio-button nzValue="circle">Circle</label>
<label nz-radio-button nzValue="square">Square</label>
</nz-radio-group>
</div>
</div>
<br />
<nz-skeleton-element nzType="avatar" [nzActive]="avatarActive" [nzSize]="avatarSize" [nzShape]="avatarShape"></nz-skeleton-element>
<br />
<br />
<div nz-row nzAlign="middle" [nzGutter]="8">
<div nz-col nzSpan="5">
InputActive:
<nz-switch [(ngModel)]="inputActive"></nz-switch>
</div>
<div nz-col nzSpan="9">
InputSize:
<nz-radio-group [(ngModel)]="inputSize">
<label nz-radio-button nzValue="default">Default</label>
<label nz-radio-button nzValue="large">Large</label>
<label nz-radio-button nzValue="small">Small</label>
</nz-radio-group>
</div>
</div>
<br />
<nz-skeleton-element nzType="input" [nzActive]="inputActive" [nzSize]="inputSize" style="width:300px"></nz-skeleton-element>
`
})
export class NzDemoSkeletonElementComponent {
buttonActive = false;
avatarActive = false;
inputActive = false;
buttonSize = 'default';
avatarSize = 'default';
inputSize = 'default';
buttonShape = 'default';
avatarShape = 'circle';
}
包含子组件
加载占位图包含子组件。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-skeleton-children',
template: `
<div class="article">
<nz-skeleton [nzLoading]="loading">
<h4>Ant Design, a design language</h4>
<p>
We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people
create their product prototypes beautifully and efficiently.
</p>
</nz-skeleton>
<button nz-button (click)="showSkeleton()" [disabled]="loading">
Show Skeleton
</button>
</div>
`,
styles: [
`
.article h4 {
margin-bottom: 16px;
}
.article button {
margin-top: 16px;
}
`
]
})
export class NzDemoSkeletonChildrenComponent {
loading = false;
showSkeleton(): void {
this.loading = true;
setTimeout(() => {
this.loading = false;
}, 3000);
}
}
列表样例
在列表组件中使用加载占位符。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-skeleton-list',
template: `
<nz-switch [(ngModel)]="loading"></nz-switch>
<nz-list [nzDataSource]="listData" [nzRenderItem]="item" [nzItemLayout]="'vertical'">
<ng-template #item let-item>
<nz-list-item
[nzContent]="loading ? ' ' : item.content"
[nzActions]="loading ? [] : [starAction, likeAction, msgAction]"
[nzExtra]="loading ? '' : extra"
>
<nz-skeleton [nzLoading]="loading" [nzActive]="true" [nzAvatar]="true">
<ng-template #starAction><i nz-icon nzType="star-o" style="margin-right: 8px;"></i> 156</ng-template>
<ng-template #likeAction><i nz-icon nzType="like-o" style="margin-right: 8px;"></i> 156</ng-template>
<ng-template #msgAction><i nz-icon nzType="message" style="margin-right: 8px;"></i> 2</ng-template>
<nz-list-item-meta [nzAvatar]="item.avatar" [nzTitle]="nzTitle" [nzDescription]="item.description">
<ng-template #nzTitle
><a href="{{ item.href }}">{{ item.title }}</a></ng-template
>
</nz-list-item-meta>
<ng-template #extra>
<img width="272" alt="logo" src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png" />
</ng-template>
</nz-skeleton>
</nz-list-item>
</ng-template>
</nz-list>
`
})
export class NzDemoSkeletonListComponent {
loading = true;
listData = new Array(3).fill({}).map((_i, index) => {
return {
href: 'http://ng.ant.design',
title: `ant design part ${index}`,
avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
description: 'Ant Design, a design language for background applications, is refined by Ant UED Team.',
content:
'We supply a series of design principles, practical patterns and high quality design resources ' +
'(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.'
};
});
}
API
nz-skeletoncomponent
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
[nzActive] | 是否展示动画效果 | boolean | false |
[nzAvatar] | 是否显示头像占位图 | boolean | NzSkeletonAvatar | false |
[nzLoading] | 为 true 时,显示占位图。反之则直接展示子组件 | boolean | - |
[nzParagraph] | 是否显示段落占位图 | boolean | NzSkeletonParagraph | true |
[nzTitle] | 是否显示标题占位图 | boolean | NzSkeletonTitle | true |
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] | 是否展示动画效果 | boolean | false |
[nzSize] | 大小 | ‘large’ | ‘small’ | ‘default’ | ‘default’ |
[nzShape] | 形状 | ‘circle’ | ‘round’ | ‘default’ | ‘default’ |
nz-skeleton-element [nzType=”avatar”]component
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
[nzActive] | 是否展示动画效果 | boolean | false |
[nzSize] | 大小 | number | ‘large’ | ‘small’ | ‘default’ | ‘default’ |
[nzShape] | 形状 | ‘circle’ | ‘square’ | ‘square’ |
nz-skeleton-element [nzType=”input”]component
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
[nzActive] | 是否展示动画效果 | boolean | false |
[nzSize] | 大小 | ‘large’ | ‘small’ | ‘default’ | ‘default’ |