Animate 动画
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
开发指南
何时使用
需要自定义动效
API
动画
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
animation | 配置动画的播放方式, 详见animation | String/Object | { appear: noop, enter: noop, leave: noop } |
afterAppear | 在初始动画播放完毕触发的事件签名:Function() => void | Function | () => {} |
afterEnter | 在进场动画播放完毕触发的事件签名:Function() => void | Function | () => {} |
afterLeave | 在离开动画播放完毕触发的事件签名:Function() => void | Function | () => {} |
component | 在针对多个子节点播放动画的时候包裹的标签 | any | - |
singleMode | 是否是有单个节点,如果有多个动画的孩子节点,设置该选项为false | Boolean | true |
animationAppear | 是否在初始的时候播放动画 | Boolean | true |
代码示例
动效API使用
查看源码在线预览
import { Animate, Button, Select } from "@icedesign/base";
class App extends React.Component {
state = {
enter: "fadeInLeft",
leave: "fadeOutRight",
visible: false
};
componentDidMount() {
this.onClick();
}
render() {
const animation = {
enter: this.state.enter,
leave: this.state.leave
};
return (
<div>
<Animate
singleMode={false}
animation={animation}
afterEnter={this.afterEnter}
afterLeave={this.afterLeave}
className="demo"
>
{this.state.visible ? <div>Animate</div> : null}
</Animate>
<Selecter
defaultValue={{ in: this.state.enter, out: this.state.leave }}
onChange={this.onChange}
/>
<Button onClick={this.onClick}>触发</Button>
</div>
);
}
onClick = () => {
this.setState({
visible: true
});
setTimeout(() => {
this.setState({
visible: false
});
}, 600);
};
onChange = animation => {
this.setState({
enter: animation.in,
leave: animation.out
});
this.onClick();
};
afterEnter() {
console.log("afterEnter");
}
afterLeave() {
console.log("afterLeave");
}
}
/* eslint-disable react/no-multi-comp */
class Selecter extends React.Component {
constructor(props) {
super(props);
if (props.defaultValue) {
this.state = {
in: props.defaultValue.in,
out: props.defaultValue.out
};
} else {
this.state = {
in: "fadeInLeft",
out: "fadeOutRight"
};
}
}
render() {
return (
<div className="selecter">
<br />
<span>入场动画: </span>
<Select value={this.state.in} onChange={this.onChangeIn}>
{Animate.names.enter.map(i => {
return (
<option key={i} value={i}>
{i}
</option>
);
})}
</Select>
<span> 出场动画: </span>
<Select value={this.state.out} onChange={this.onChangeOut}>
{Animate.names.leave.map(i => {
return (
<option key={i} value={i}>
{i}
</option>
);
})}
</Select>
<br />
<br />
</div>
);
}
onChangeIn = v => {
let animation = { ...this.state, in: v };
this.setState(animation);
this.props.onChange && this.props.onChange(animation);
};
onChangeOut = v => {
let animation = { ...this.state, out: v };
this.setState(animation);
this.props.onChange && this.props.onChange(animation);
};
}
ReactDOM.render(<App />, mountNode);
.demo {
color: #00BCD4;
display: block;
height: 100px;
line-height: 100px;
text-align: center;
font-size: 36px;
background-color: #ddd;
}
.selecter .next-select {
vertical-align: middle;
}
className 用法
查看源码在线预览
import { Animate, Button, Select } from "@icedesign/base";
class App extends React.Component {
state = {
enter: "fadeInLeftBig",
leave: "fadeOutRightBig",
className: ""
};
render() {
return (
<div>
<div className="demo">
<div className={this.state.className}>Aniamte</div>
</div>
<Selecter
defaultValue={{ in: this.state.enter, out: this.state.leave }}
onChange={this.onChange}
/>
<Button onClick={this.onClick}>触发</Button>
</div>
);
}
onClick = () => {
this.setState({
className: this.state.enter
});
setTimeout(() => {
this.setState({
className: this.state.leave
});
setTimeout(() => {
this.setState({
className: ""
});
}, 300);
}, 300);
};
onChange = animation => {
this.setState({
enter: animation.in,
leave: animation.out,
className: animation.in
});
setTimeout(() => {
this.onClick();
}, 0);
};
}
/* eslint-disable react/no-multi-comp */
class Selecter extends React.Component {
constructor(props) {
super(props);
if (props.defaultValue) {
this.state = {
in: props.defaultValue.in,
out: props.defaultValue.out
};
} else {
this.state = {
in: "fadeInLeft",
out: "fadeOutRight"
};
}
}
render() {
return (
<div className="selecter">
<br />
<span>入场动画: </span>
<Select value={this.state.in} onChange={this.onChangeIn}>
{Animate.names.enter.map(i => {
return (
<option key={i} value={i}>
{i}
</option>
);
})}
</Select>
<span> 出场动画: </span>
<Select value={this.state.out} onChange={this.onChangeOut}>
{Animate.names.leave.map(i => {
return (
<option key={i} value={i}>
{i}
</option>
);
})}
</Select>
<br />
<br />
</div>
);
}
onChangeIn = v => {
let animation = { ...this.state, in: v };
this.setState(animation);
this.props.onChange && this.props.onChange(animation);
};
onChangeOut = v => {
let animation = { ...this.state, out: v };
this.setState(animation);
this.props.onChange && this.props.onChange(animation);
};
}
ReactDOM.render(<App />, mountNode);
.demo {
color: #00BCD4;
display: block;
height: 100px;
line-height: 100px;
text-align: center;
font-size: 36px;
background-color: #ddd;
}
.selecter .next-select {
vertical-align: middle;
}
入场出场
查看源码在线预览
import { Animate } from "@icedesign/base";
const animation = {
enter: "expandInDown",
leave: "expandOutUp"
};
class App extends React.Component {
state = {
items: []
};
render() {
return (
<div className="demo2">
<div className="element">
<div className="addbtn" onClick={this.addItem.bind(this)}>
Add item
</div>
<Animate
singleMode={false}
animation={animation}
afterEnter={this.afterEnter}
afterLeave={this.afterLeave}
>
{this.state.items}
</Animate>
</div>
</div>
);
}
addItem() {
let { items } = this.state,
key = Date.now(),
item = (
<div key={key}>
{" "}
<a href="javascript:;" onClick={this.removeItem.bind(this, key)}>
Remove
</a>
</div>
);
items.push(item);
this.setState({ items });
}
removeItem(key) {
let { items } = this.state,
i,
list = items.slice();
list.forEach((item, index) => {
if (item.key == key) {
i = index;
}
});
if (i > -1) {
list.splice(i, 1);
this.setState({ items: list });
}
}
afterEnter() {
console.log("afterEnter");
}
afterLeave() {
console.log("afterLeave");
}
}
ReactDOM.render(<App />, mountNode);
.demo2 {
color: #4C4C4C;
height: 400px;
background-color: #eee;
margin: 0 auto;
padding-top: 30px;
}
.element{
width: 300px;
margin: 70px auto;
color: #000;
text-align: center;
}
.element span div{
height: 30px;
background-color: #4C4C4C;
border-bottom: 1px solid #eee;
cursor: pointer;
transition: all 1s;
}
.addbtn{
cursor: pointer;
background-color: #2196F3;
height: 40px;
line-height: 40px;
}
.element a {
color: white;
line-height: 30px;
}