Skeleton骨架屏
在需要等待加载内容的位置提供一个占位图形组合。
何时使用
网络较慢,需要长时间等待加载处理的情况下。
图文信息内容较多的列表/卡片中。
只在第一次加载数据的时候使用。
可以被 Spin 完全代替,但是在可用的场景下可以比 Spin 提供更好的视觉效果和用户体验。
代码演示
最简单的占位效果。
import { Skeleton } from 'antd';
ReactDOM.render(<Skeleton />, mountNode);
更复杂的组合。
import { Skeleton } from 'antd';
ReactDOM.render(<Skeleton avatar paragraph={{ rows: 4 }} />, mountNode);
显示动画效果。
import { Skeleton } from 'antd';
ReactDOM.render(<Skeleton active />, mountNode);
骨架按钮、头像和输入框。
import { Skeleton, Switch, Form, Radio } from 'antd';
class Demo extends React.Component {
state = {
buttonActive: false,
avatarActive: false,
inputActive: false,
buttonSize: 'default',
avatarSize: 'default',
inputSize: 'default',
buttonShape: 'default',
avatarShape: 'circle',
};
handleActiveChange = prop => checked => {
this.setState({ [prop]: checked });
};
handleSizeChange = prop => e => {
this.setState({ [prop]: e.target.value });
};
handleShapeChange = prop => e => {
this.setState({ [prop]: e.target.value });
};
render() {
const {
buttonActive,
avatarActive,
inputActive,
buttonSize,
avatarSize,
inputSize,
buttonShape,
avatarShape,
} = this.state;
return (
<div>
<div>
<Form layout="inline" style={{ marginBottom: 16 }}>
<Form.Item label="ButtonActive">
<Switch checked={buttonActive} onChange={this.handleActiveChange('buttonActive')} />
</Form.Item>
<Form.Item label="ButtonSize">
<Radio.Group value={buttonSize} onChange={this.handleSizeChange('buttonSize')}>
<Radio.Button value="default">Default</Radio.Button>
<Radio.Button value="large">Large</Radio.Button>
<Radio.Button value="small">Small</Radio.Button>
</Radio.Group>
</Form.Item>
<Form.Item label="ButtonShape">
<Radio.Group value={buttonShape} onChange={this.handleShapeChange('buttonShape')}>
<Radio.Button value="default">Default</Radio.Button>
<Radio.Button value="round">Round</Radio.Button>
<Radio.Button value="circle">Circle</Radio.Button>
</Radio.Group>
</Form.Item>
</Form>
<Skeleton.Button active={buttonActive} size={buttonSize} shape={buttonShape} />
</div>
<br />
<div>
<Form layout="inline" style={{ marginBottom: 16 }}>
<Form.Item label="AvatarActive">
<Switch checked={avatarActive} onChange={this.handleActiveChange('avatarActive')} />
</Form.Item>
<Form.Item label="AvatarSize">
<Radio.Group value={avatarSize} onChange={this.handleSizeChange('avatarSize')}>
<Radio.Button value="default">Default</Radio.Button>
<Radio.Button value="large">Large</Radio.Button>
<Radio.Button value="small">Small</Radio.Button>
</Radio.Group>
</Form.Item>
<Form.Item label="AvatarShape">
<Radio.Group value={avatarShape} onChange={this.handleShapeChange('avatarShape')}>
<Radio.Button value="square">Square</Radio.Button>
<Radio.Button value="circle">Circle</Radio.Button>
</Radio.Group>
</Form.Item>
</Form>
<Skeleton.Avatar active={avatarActive} size={avatarSize} shape={avatarShape} />
</div>
<br />
<div>
<Form layout="inline" style={{ marginBottom: 16 }}>
<Form.Item label="InputActive">
<Switch checked={inputActive} onChange={this.handleActiveChange('inputActive')} />
</Form.Item>
<Form.Item label="InputSize">
<Radio.Group value={inputSize} onChange={this.handleSizeChange('inputSize')}>
<Radio.Button value="default">Default</Radio.Button>
<Radio.Button value="large">Large</Radio.Button>
<Radio.Button value="small">Small</Radio.Button>
</Radio.Group>
</Form.Item>
</Form>
<Skeleton.Input style={{ width: '300px' }} active={inputActive} size={inputSize} />
</div>
</div>
);
}
}
ReactDOM.render(<Demo />, mountNode);
加载占位图包含子组件。
import { Skeleton, Button } from 'antd';
class Demo extends React.Component {
state = {
loading: false,
};
showSkeleton = () => {
this.setState({ loading: true });
setTimeout(() => {
this.setState({ loading: false });
}, 3000);
};
render() {
return (
<div className="article">
<Skeleton loading={this.state.loading}>
<div>
<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>
</div>
</Skeleton>
<Button onClick={this.showSkeleton} disabled={this.state.loading}>
Show Skeleton
</Button>
</div>
);
}
}
ReactDOM.render(<Demo />, mountNode);
在列表组件中使用加载占位符。
import { Skeleton, Switch, List, Avatar } from 'antd';
import { StarOutlined, LikeOutlined, MessageOutlined } from '@ant-design/icons';
const listData = [];
for (let i = 0; i < 3; i++) {
listData.push({
href: 'https://ant.design',
title: `ant design part ${i}`,
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.',
});
}
const IconText = ({ icon, text }) => (
<span>
{React.createElement(icon, { style: { marginRight: 8 } })}
{text}
</span>
);
class App extends React.Component {
state = {
loading: true,
};
onChange = checked => {
this.setState({ loading: !checked });
};
render() {
const { loading } = this.state;
return (
<div>
<Switch checked={!loading} onChange={this.onChange} />
<List
itemLayout="vertical"
size="large"
dataSource={listData}
renderItem={item => (
<List.Item
key={item.title}
actions={
!loading && [
<IconText icon={StarOutlined} text="156" key="list-vertical-star-o" />,
<IconText icon={LikeOutlined} text="156" key="list-vertical-like-o" />,
<IconText icon={MessageOutlined} text="2" key="list-vertical-message" />,
]
}
extra={
!loading && (
<img
width={272}
alt="logo"
src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png"
/>
)
}
>
<Skeleton loading={loading} active avatar>
<List.Item.Meta
avatar={<Avatar src={item.avatar} />}
title={<a href={item.href}>{item.title}</a>}
description={item.description}
/>
{item.content}
</Skeleton>
</List.Item>
)}
/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
API
Skeleton
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
active | 是否展示动画效果 | boolean | false |
avatar | 是否显示头像占位图 | boolean | SkeletonAvatarProps | false |
loading | 为 true 时,显示占位图。反之则直接展示子组件 | boolean | - |
paragraph | 是否显示段落占位图 | boolean | SkeletonParagraphProps | true |
title | 是否显示标题占位图 | boolean | SkeletonTitleProps | true |
round | 为 true 时,段落和标题显示圆角 | boolean | false |
SkeletonAvatarProps
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
active | 是否展示动画效果,仅在单独使用头像骨架时生效 | boolean | false |
size | 设置头像占位图的大小 | number | large | small | default | - |
shape | 指定头像的形状 | circle | square | - |
SkeletonTitleProps
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
width | 设置标题占位图的宽度 | number | string | - |
SkeletonParagraphProps
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
rows | 设置段落占位图的行数 | number | - |
width | 设置段落占位图的宽度,若为数组时则为对应的每行宽度,反之则是最后一行的宽度 | number | string | Array<number | string> | - |
SkeletonButtonProps
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
active | 是否展示动画效果 | boolean | false |
size | 设置按钮的大小 | large | small | default | - |
shape | 指定按钮的形状 | circle | round | default | - |
SkeletonInputProps
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
active | 是否展示动画效果 | boolean | false |
size | 设置按钮的大小 | large | small | default | - |