Loading 加载

如果项目中使用的是 0.x 版本的基础组件(@icedesign/base, @ali/ice, @alife/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令npm install @alifd/next@latest -S

开发指南

何时使用

页面局部处于等待异步数据或正在渲染过程时,合适的加载动效会有效缓解用户的焦虑。

注意事项

Loading 默认使用 display='inline-block' 布局的方式包裹内部元素。

如果希望 通栏包裹 可以修改 <Loading style={{display: 'block'}} />

API

Loading

参数说明类型默认值
tip自定义内容any-
size设置动画尺寸可选值:'large'(大号)'medium'(中号)Enum'large'
tipAlign自定义内容位置可选值:'right'(出现在动画右边)'bottom'(出现在动画下面)Enum'bottom'
visibleloading 状态, 默认 trueBooleantrue
indicator自定义动画any-
color动画颜色String-
fullScreen全屏展示Boolean-
children子元素any-
inlineshould loader be displayed inlineBooleantrue

代码示例

基本用法

最简单的用法。

Loading 加载 - 图1

查看源码在线预览

  1. import { Loading } from '@alifd/next';
  2. ReactDOM.render(
  3. <div>
  4. <div>LTR
  5. <Loading tip="加载中..." >
  6. <div className="demo">test</div>
  7. </Loading>
  8. </div>
  9. <div>RTL
  10. <Loading tip="加载中..." rtl >
  11. <div className="demo">test</div>
  12. </Loading>
  13. </div>
  14. </div>
  15. , mountNode);
  1. .demo {
  2. width: 500px;
  3. background-color: #F2F3F7;
  4. text-align: center;
  5. padding:50px;
  6. }

自定义Loading动画

你可以自定义动画,把自己的动画元素传进去, 需要自己写动画样式

Loading 加载 - 图2

查看源码在线预览

  1. import { Loading } from '@alifd/next';
  2. const indicator1 = (<div className="load-container load1">
  3. <div className="loader">loading...</div>
  4. </div>);
  5. const indicator7 = (<div className="load-container load7">
  6. <div className="loader">loading...</div>
  7. </div>);
  8. ReactDOM.render(<div>
  9. <Loading tip="default animation">
  10. <div className="demo">test</div>
  11. </Loading>
  12. <Loading indicator={indicator1}>
  13. <div className="demo">test</div>
  14. </Loading>
  15. <Loading indicator={indicator7}>
  16. <div className="demo">test</div>
  17. </Loading>
  18. </div>, mountNode);
  1. .demo {
  2. width: 500px;
  3. background-color: #F2F3F7;
  4. text-align: center;
  5. padding:50px;
  6. }
  7. .next-loading {
  8. margin-bottom: 5px;
  9. }
  10. /* start copy from here */
  11. .load-container {
  12. margin: 0 auto;
  13. position: relative;
  14. }
  15. /* animation 1 */
  16. .load1 .loader,
  17. .load1 .loader:before,
  18. .load1 .loader:after {
  19. background: #5584FF;
  20. color: #5584FF;
  21. -webkit-animation: load1 1s infinite ease-in-out;
  22. animation: load1 1s infinite ease-in-out;
  23. width: 1em;
  24. height: 4em;
  25. }
  26. .load1 .loader:before,
  27. .load1 .loader:after {
  28. position: absolute;
  29. top: 0;
  30. content: '';
  31. }
  32. .load1 .loader:before {
  33. left: -1.5em;
  34. -webkit-animation-delay: -0.32s;
  35. animation-delay: -0.32s;
  36. }
  37. .load1 .loader {
  38. text-indent: -9999em;
  39. margin: 0 auto !important;
  40. position: relative;
  41. font-size: 11px;
  42. -webkit-animation-delay: -0.16s;
  43. animation-delay: -0.16s;
  44. }
  45. .load1 .loader:after {
  46. left: 1.5em;
  47. }
  48. @-webkit-keyframes load1 {
  49. 0%,
  50. 80%,
  51. 100% {
  52. box-shadow: 0 0 ;
  53. height: 4em;
  54. }
  55. 40% {
  56. box-shadow: 0 -2em ;
  57. height: 5em;
  58. }
  59. }
  60. @keyframes load1 {
  61. 0%,
  62. 80%,
  63. 100% {
  64. box-shadow: 0 0 ;
  65. height: 4em;
  66. }
  67. 40% {
  68. box-shadow: 0 -2em ;
  69. height: 5em;
  70. }
  71. }
  72. /* animation 2 */
  73. .load7 .loader:before,
  74. .load7 .loader:after,
  75. .load7 .loader {
  76. border-radius: 50%;
  77. width: 2.5em;
  78. height: 2.5em;
  79. -webkit-animation-fill-mode: both;
  80. animation-fill-mode: both;
  81. -webkit-animation: load7 1.8s infinite ease-in-out;
  82. animation: load7 1.8s infinite ease-in-out;
  83. top: -36px;
  84. }
  85. .load7 .loader {
  86. margin: 0 auto !important;
  87. font-size: 10px;
  88. position: relative;
  89. text-indent: -9999em;
  90. -webkit-animation-delay: -0.16s;
  91. animation-delay: -0.16s;
  92. }
  93. .load7 .loader:before {
  94. left: -3.5em;
  95. -webkit-animation-delay: -0.32s;
  96. animation-delay: -0.32s;
  97. }
  98. .load7 .loader:after {
  99. left: 3.5em;
  100. }
  101. .load7 .loader:before,
  102. .load7 .loader:after {
  103. content: '';
  104. position: absolute;
  105. top: 0;
  106. }
  107. @-webkit-keyframes load7 {
  108. 0%,
  109. 80%,
  110. 100% {
  111. box-shadow: 0 2.5em 0 -1.3em #5584FF;
  112. }
  113. 40% {
  114. box-shadow: 0 2.5em 0 0 #5584FF;
  115. }
  116. }
  117. @keyframes load7 {
  118. 0%,
  119. 80%,
  120. 100% {
  121. box-shadow: 0 2.5em 0 -1.3em #5584FF;
  122. }
  123. 40% {
  124. box-shadow: 0 2.5em 0 0 #5584FF;
  125. }
  126. }

关闭加载

可切换加载状态。

Loading 加载 - 图3

查看源码在线预览

  1. import { Loading, Form, Input, Button } from '@alifd/next';
  2. const FormItem = Form.Item;
  3. const layout = {
  4. labelCol: {
  5. fixedSpan: 4
  6. },
  7. wrapperCol: {
  8. span: 18
  9. }
  10. };
  11. class App extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. visible: false
  16. };
  17. }
  18. setVisible(visible) {
  19. this.setState({
  20. visible
  21. });
  22. }
  23. render() {
  24. return (<div>
  25. <Loading visible={this.state.visible} shape="fusion-reactor">
  26. <Form style={{width: 500}}>
  27. <FormItem label="Username" {...layout} >
  28. <Input />
  29. </FormItem>
  30. <FormItem label="Password" {...layout} >
  31. <Input htmlType="password" placeholder="please input"/>
  32. </FormItem>
  33. <FormItem label="Detail" {...layout} >
  34. <Input multiple />
  35. </FormItem>
  36. </Form>
  37. </Loading>
  38. <div style={{paddingLeft: 80}}>
  39. <Button onClick={this.setVisible.bind(this, true)} type="primary">Submit</Button>
  40. <Button onClick={this.setVisible.bind(this, false)} style={{marginLeft: 5}}>Cancel</Button>
  41. </div>
  42. </div>);
  43. }
  44. }
  45. ReactDOM.render(<App/>, mountNode);

全屏

全屏展示

Loading 加载 - 图4

查看源码在线预览

  1. import { Loading, Button } from '@alifd/next';
  2. class App extends React.Component {
  3. constructor(props, context) {
  4. super(props, context);
  5. this.state = {
  6. visible: false
  7. };
  8. }
  9. onClick() {
  10. this.setState({visible: !this.state.visible});
  11. }
  12. onClose = () => {
  13. this.setState({
  14. visible: false
  15. });
  16. }
  17. render() {
  18. return (
  19. <span>
  20. <Loading visible={this.state.visible}
  21. fullScreen
  22. shape="fusion-reactor"
  23. safeNode={this.btn}
  24. onVisibleChange={this.onClose.bind(this)}>
  25. <Button onClick={this.onClick.bind(this)} ref={ref => (this.btn = ref)}>Full Screen</Button>
  26. </Loading>
  27. </span>
  28. );
  29. }
  30. }
  31. ReactDOM.render(<App/>, mountNode);

自定义提示语位置

你可以选择提示语的位置,目前支持两个值 right/bottom(默认值)

Loading 加载 - 图5

查看源码在线预览

  1. import { Loading } from '@alifd/next';
  2. ReactDOM.render(<div>
  3. <Loading tip="default">
  4. <div className="demo">test</div>
  5. </Loading>
  6. <Loading tip="right" tipAlign="right">
  7. <div className="demo">test</div>
  8. </Loading>
  9. <Loading tip="bottom" tipAlign="bottom">
  10. <div className="demo">test</div>
  11. </Loading>
  12. </div>, mountNode);
  1. .demo {
  2. width: 500px;
  3. background-color: #F2F3F7;
  4. text-align: center;
  5. padding:50px;
  6. }
  7. .next-loading {
  8. margin-bottom: 5px;
  9. }

Loading动画尺寸

设置Loading动画的尺寸,只对原生的indicator管用

Loading 加载 - 图6

查看源码在线预览

  1. import { Loading } from '@alifd/next';
  2. ReactDOM.render(<div>
  3. <Loading tip="normal(size default large)" >
  4. <div className="demo">test</div>
  5. </Loading>
  6. <Loading tip="large" size="large">
  7. <div className="demo">test</div>
  8. </Loading>
  9. <Loading tip="medium" size="medium">
  10. <div className="demo">test</div>
  11. </Loading>
  12. </div>, mountNode);
  1. .demo {
  2. width: 500px;
  3. background-color: #F2F3F7;
  4. text-align: center;
  5. padding:50px;
  6. }
  7. .next-loading {
  8. margin-bottom: 5px;
  9. }

相关区块

Loading 加载 - 图7

暂无相关区块