Skeleton骨架屏

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

何时使用

  • 网络较慢,需要长时间等待加载处理的情况下。

  • 图文信息内容较多的列表/卡片中。

  • 只在第一次加载数据的时候使用。

  • 可以被 Spin 完全代替,但是在可用的场景下可以比 Spin 提供更好的视觉效果和用户体验。

代码演示

Skeleton骨架屏 - 图1

基本

最简单的占位效果。

  1. import { Skeleton } from 'antd';
  2. ReactDOM.render(<Skeleton />, mountNode);

Skeleton骨架屏 - 图2

复杂的组合

更复杂的组合。

  1. import { Skeleton } from 'antd';
  2. ReactDOM.render(<Skeleton avatar paragraph={{ rows: 4 }} />, mountNode);

Skeleton骨架屏 - 图3

动画效果

显示动画效果。

  1. import { Skeleton } from 'antd';
  2. ReactDOM.render(<Skeleton active />, mountNode);

Skeleton骨架屏 - 图4

按钮/头像/输入框/图像

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

  1. import { Skeleton, Space, Divider, Switch, Form, Radio } from 'antd';
  2. class Demo extends React.Component {
  3. state = {
  4. active: false,
  5. size: 'default',
  6. buttonShape: 'default',
  7. avatarShape: 'circle',
  8. };
  9. handleActiveChange = checked => {
  10. this.setState({ active: checked });
  11. };
  12. handleSizeChange = e => {
  13. this.setState({ size: e.target.value });
  14. };
  15. handleShapeChange = prop => e => {
  16. this.setState({ [prop]: e.target.value });
  17. };
  18. render() {
  19. const { active, size, buttonShape, avatarShape } = this.state;
  20. return (
  21. <>
  22. <Space>
  23. <Skeleton.Button active={active} size={size} shape={buttonShape} />
  24. <Skeleton.Button active={active} size={size} shape={buttonShape} />
  25. <Skeleton.Avatar active={active} size={size} shape={avatarShape} />
  26. <Skeleton.Input style={{ width: 200 }} active={active} size={size} />
  27. </Space>
  28. <br />
  29. <br />
  30. <Skeleton.Image />
  31. <Divider />
  32. <Form layout="inline" style={{ margin: '16px 0' }}>
  33. <Form.Item label="Active">
  34. <Switch checked={active} onChange={this.handleActiveChange} />
  35. </Form.Item>
  36. <Form.Item label="Size">
  37. <Radio.Group value={size} onChange={this.handleSizeChange}>
  38. <Radio.Button value="default">Default</Radio.Button>
  39. <Radio.Button value="large">Large</Radio.Button>
  40. <Radio.Button value="small">Small</Radio.Button>
  41. </Radio.Group>
  42. </Form.Item>
  43. <Form.Item label="Button Shape">
  44. <Radio.Group value={buttonShape} onChange={this.handleShapeChange('buttonShape')}>
  45. <Radio.Button value="default">Default</Radio.Button>
  46. <Radio.Button value="round">Round</Radio.Button>
  47. <Radio.Button value="circle">Circle</Radio.Button>
  48. </Radio.Group>
  49. </Form.Item>
  50. <Form.Item label="Avatar Shape">
  51. <Radio.Group value={avatarShape} onChange={this.handleShapeChange('avatarShape')}>
  52. <Radio.Button value="square">Square</Radio.Button>
  53. <Radio.Button value="circle">Circle</Radio.Button>
  54. </Radio.Group>
  55. </Form.Item>
  56. </Form>
  57. </>
  58. );
  59. }
  60. }
  61. ReactDOM.render(<Demo />, mountNode);

Skeleton骨架屏 - 图5

包含子组件

加载占位图包含子组件。

  1. import { Skeleton, Button } from 'antd';
  2. class Demo extends React.Component {
  3. state = {
  4. loading: false,
  5. };
  6. showSkeleton = () => {
  7. this.setState({ loading: true });
  8. setTimeout(() => {
  9. this.setState({ loading: false });
  10. }, 3000);
  11. };
  12. render() {
  13. return (
  14. <div className="article">
  15. <Skeleton loading={this.state.loading}>
  16. <div>
  17. <h4>Ant Design, a design language</h4>
  18. <p>
  19. We supply a series of design principles, practical patterns and high quality design
  20. resources (Sketch and Axure), to help people create their product prototypes
  21. beautifully and efficiently.
  22. </p>
  23. </div>
  24. </Skeleton>
  25. <Button onClick={this.showSkeleton} disabled={this.state.loading}>
  26. Show Skeleton
  27. </Button>
  28. </div>
  29. );
  30. }
  31. }
  32. ReactDOM.render(<Demo />, mountNode);

Skeleton骨架屏 - 图6

列表

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

  1. import { Skeleton, Switch, List, Avatar } from 'antd';
  2. import { StarOutlined, LikeOutlined, MessageOutlined } from '@ant-design/icons';
  3. const listData = [];
  4. for (let i = 0; i < 3; i++) {
  5. listData.push({
  6. href: 'https://ant.design',
  7. title: `ant design part ${i}`,
  8. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  9. description:
  10. 'Ant Design, a design language for background applications, is refined by Ant UED Team.',
  11. content:
  12. '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.',
  13. });
  14. }
  15. const IconText = ({ icon, text }) => (
  16. <span>
  17. {React.createElement(icon, { style: { marginRight: 8 } })}
  18. {text}
  19. </span>
  20. );
  21. class App extends React.Component {
  22. state = {
  23. loading: true,
  24. };
  25. onChange = checked => {
  26. this.setState({ loading: !checked });
  27. };
  28. render() {
  29. const { loading } = this.state;
  30. return (
  31. <>
  32. <Switch checked={!loading} onChange={this.onChange} />
  33. <List
  34. itemLayout="vertical"
  35. size="large"
  36. dataSource={listData}
  37. renderItem={item => (
  38. <List.Item
  39. key={item.title}
  40. actions={
  41. !loading && [
  42. <IconText icon={StarOutlined} text="156" key="list-vertical-star-o" />,
  43. <IconText icon={LikeOutlined} text="156" key="list-vertical-like-o" />,
  44. <IconText icon={MessageOutlined} text="2" key="list-vertical-message" />,
  45. ]
  46. }
  47. extra={
  48. !loading && (
  49. <img
  50. width={272}
  51. alt="logo"
  52. src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png"
  53. />
  54. )
  55. }
  56. >
  57. <Skeleton loading={loading} active avatar>
  58. <List.Item.Meta
  59. avatar={<Avatar src={item.avatar} />}
  60. title={<a href={item.href}>{item.title}</a>}
  61. description={item.description}
  62. />
  63. {item.content}
  64. </Skeleton>
  65. </List.Item>
  66. )}
  67. />
  68. </>
  69. );
  70. }
  71. }
  72. ReactDOM.render(<App />, mountNode);

API

Skeleton

属性说明类型默认值
active是否展示动画效果booleanfalse
avatar是否显示头像占位图boolean | SkeletonAvatarPropsfalse
loading为 true 时,显示占位图。反之则直接展示子组件boolean-
paragraph是否显示段落占位图boolean | SkeletonParagraphPropstrue
round为 true 时,段落和标题显示圆角booleanfalse
title是否显示标题占位图boolean | SkeletonTitlePropstrue

SkeletonAvatarProps

属性说明类型默认值
active是否展示动画效果,仅在单独使用头像骨架时生效booleanfalse
shape指定头像的形状circle | square-
size设置头像占位图的大小number | large | small | default-

SkeletonTitleProps

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

SkeletonParagraphProps

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

SkeletonButtonProps

属性说明类型默认值
active是否展示动画效果booleanfalse
shape指定按钮的形状circle | round | default-
size设置按钮的大小large | small | default-

SkeletonInputProps

属性说明类型默认值
active是否展示动画效果booleanfalse
size设置输入框的大小large | small | default-