Loading 加载
如果项目中使用的是 0.x 版本的基础组件(@icedesign/base, @ali/ice, @alife/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
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' |
visible | loading 状态, 默认 true | Boolean | true |
indicator | 自定义动画 | any | - |
color | 动画颜色 | String | - |
fullScreen | 全屏展示 | Boolean | - |
children | 子元素 | any | - |
inline | should loader be displayed inline | Boolean | true |
代码示例
最简单的用法。
查看源码在线预览
import { Loading } from '@alifd/next';
ReactDOM.render(
<div>
<div>LTR
<Loading tip="加载中..." >
<div className="demo">test</div>
</Loading>
</div>
<div>RTL
<Loading tip="加载中..." rtl >
<div className="demo">test</div>
</Loading>
</div>
</div>
, mountNode);
.demo {
width: 500px;
background-color: #F2F3F7;
text-align: center;
padding:50px;
}
你可以自定义动画,把自己的动画元素传进去, 需要自己写动画样式
查看源码在线预览
import { Loading } from '@alifd/next';
const indicator1 = (<div className="load-container load1">
<div className="loader">loading...</div>
</div>);
const indicator7 = (<div className="load-container load7">
<div className="loader">loading...</div>
</div>);
ReactDOM.render(<div>
<Loading tip="default animation">
<div className="demo">test</div>
</Loading>
<Loading indicator={indicator1}>
<div className="demo">test</div>
</Loading>
<Loading indicator={indicator7}>
<div className="demo">test</div>
</Loading>
</div>, mountNode);
.demo {
width: 500px;
background-color: #F2F3F7;
text-align: center;
padding:50px;
}
.next-loading {
margin-bottom: 5px;
}
/* start copy from here */
.load-container {
margin: 0 auto;
position: relative;
}
/* animation 1 */
.load1 .loader,
.load1 .loader:before,
.load1 .loader:after {
background: #5584FF;
color: #5584FF;
-webkit-animation: load1 1s infinite ease-in-out;
animation: load1 1s infinite ease-in-out;
width: 1em;
height: 4em;
}
.load1 .loader:before,
.load1 .loader:after {
position: absolute;
top: 0;
content: '';
}
.load1 .loader:before {
left: -1.5em;
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
.load1 .loader {
text-indent: -9999em;
margin: 0 auto !important;
position: relative;
font-size: 11px;
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
.load1 .loader:after {
left: 1.5em;
}
@-webkit-keyframes load1 {
0%,
80%,
100% {
box-shadow: 0 0 ;
height: 4em;
}
40% {
box-shadow: 0 -2em ;
height: 5em;
}
}
@keyframes load1 {
0%,
80%,
100% {
box-shadow: 0 0 ;
height: 4em;
}
40% {
box-shadow: 0 -2em ;
height: 5em;
}
}
/* animation 2 */
.load7 .loader:before,
.load7 .loader:after,
.load7 .loader {
border-radius: 50%;
width: 2.5em;
height: 2.5em;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
-webkit-animation: load7 1.8s infinite ease-in-out;
animation: load7 1.8s infinite ease-in-out;
top: -36px;
}
.load7 .loader {
margin: 0 auto !important;
font-size: 10px;
position: relative;
text-indent: -9999em;
-webkit-animation-delay: -0.16s;
animation-delay: -0.16s;
}
.load7 .loader:before {
left: -3.5em;
-webkit-animation-delay: -0.32s;
animation-delay: -0.32s;
}
.load7 .loader:after {
left: 3.5em;
}
.load7 .loader:before,
.load7 .loader:after {
content: '';
position: absolute;
top: 0;
}
@-webkit-keyframes load7 {
0%,
80%,
100% {
box-shadow: 0 2.5em 0 -1.3em #5584FF;
}
40% {
box-shadow: 0 2.5em 0 0 #5584FF;
}
}
@keyframes load7 {
0%,
80%,
100% {
box-shadow: 0 2.5em 0 -1.3em #5584FF;
}
40% {
box-shadow: 0 2.5em 0 0 #5584FF;
}
}
可切换加载状态。
查看源码在线预览
import { Loading, Form, Input, Button } from '@alifd/next';
const FormItem = Form.Item;
const layout = {
labelCol: {
fixedSpan: 4
},
wrapperCol: {
span: 18
}
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
visible: false
};
}
setVisible(visible) {
this.setState({
visible
});
}
render() {
return (<div>
<Loading visible={this.state.visible} shape="fusion-reactor">
<Form style={{width: 500}}>
<FormItem label="Username" {...layout} >
<Input />
</FormItem>
<FormItem label="Password" {...layout} >
<Input htmlType="password" placeholder="please input"/>
</FormItem>
<FormItem label="Detail" {...layout} >
<Input multiple />
</FormItem>
</Form>
</Loading>
<div style={{paddingLeft: 80}}>
<Button onClick={this.setVisible.bind(this, true)} type="primary">Submit</Button>
<Button onClick={this.setVisible.bind(this, false)} style={{marginLeft: 5}}>Cancel</Button>
</div>
</div>);
}
}
ReactDOM.render(<App/>, mountNode);
全屏展示
查看源码在线预览
import { Loading, Button } from '@alifd/next';
class App extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
visible: false
};
}
onClick() {
this.setState({visible: !this.state.visible});
}
onClose = () => {
this.setState({
visible: false
});
}
render() {
return (
<span>
<Loading visible={this.state.visible}
fullScreen
shape="fusion-reactor"
safeNode={this.btn}
onVisibleChange={this.onClose.bind(this)}>
<Button onClick={this.onClick.bind(this)} ref={ref => (this.btn = ref)}>Full Screen</Button>
</Loading>
</span>
);
}
}
ReactDOM.render(<App/>, mountNode);
你可以选择提示语的位置,目前支持两个值 right/bottom(默认值)
查看源码在线预览
import { Loading } from '@alifd/next';
ReactDOM.render(<div>
<Loading tip="default">
<div className="demo">test</div>
</Loading>
<Loading tip="right" tipAlign="right">
<div className="demo">test</div>
</Loading>
<Loading tip="bottom" tipAlign="bottom">
<div className="demo">test</div>
</Loading>
</div>, mountNode);
.demo {
width: 500px;
background-color: #F2F3F7;
text-align: center;
padding:50px;
}
.next-loading {
margin-bottom: 5px;
}
设置Loading动画的尺寸,只对原生的indicator管用
查看源码在线预览
import { Loading } from '@alifd/next';
ReactDOM.render(<div>
<Loading tip="normal(size default large)" >
<div className="demo">test</div>
</Loading>
<Loading tip="large" size="large">
<div className="demo">test</div>
</Loading>
<Loading tip="medium" size="medium">
<div className="demo">test</div>
</Loading>
</div>, mountNode);
.demo {
width: 500px;
background-color: #F2F3F7;
text-align: center;
padding:50px;
}
.next-loading {
margin-bottom: 5px;
}