Animate 动画

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

安装方法

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

开发指南

何时使用

需要自定义动效

API

动画

参数说明类型默认值
animation配置动画的播放方式, 详见animationString/Object{ appear: noop, enter: noop, leave: noop }
afterAppear在初始动画播放完毕触发的事件签名:Function() => voidFunction() => {}
afterEnter在进场动画播放完毕触发的事件签名:Function() => voidFunction() => {}
afterLeave在离开动画播放完毕触发的事件签名:Function() => voidFunction() => {}
component在针对多个子节点播放动画的时候包裹的标签any-
singleMode是否是有单个节点,如果有多个动画的孩子节点,设置该选项为falseBooleantrue
animationAppear是否在初始的时候播放动画Booleantrue

代码示例

基本

动效API使用

Animate 动画 - 图1

查看源码在线预览

  1. import { Animate, Button, Select } from "@icedesign/base";
  2. class App extends React.Component {
  3. state = {
  4. enter: "fadeInLeft",
  5. leave: "fadeOutRight",
  6. visible: false
  7. };
  8. componentDidMount() {
  9. this.onClick();
  10. }
  11. render() {
  12. const animation = {
  13. enter: this.state.enter,
  14. leave: this.state.leave
  15. };
  16. return (
  17. <div>
  18. <Animate
  19. singleMode={false}
  20. animation={animation}
  21. afterEnter={this.afterEnter}
  22. afterLeave={this.afterLeave}
  23. className="demo"
  24. >
  25. {this.state.visible ? <div>Animate</div> : null}
  26. </Animate>
  27. <Selecter
  28. defaultValue={{ in: this.state.enter, out: this.state.leave }}
  29. onChange={this.onChange}
  30. />
  31. <Button onClick={this.onClick}>触发</Button>
  32. </div>
  33. );
  34. }
  35. onClick = () => {
  36. this.setState({
  37. visible: true
  38. });
  39. setTimeout(() => {
  40. this.setState({
  41. visible: false
  42. });
  43. }, 600);
  44. };
  45. onChange = animation => {
  46. this.setState({
  47. enter: animation.in,
  48. leave: animation.out
  49. });
  50. this.onClick();
  51. };
  52. afterEnter() {
  53. console.log("afterEnter");
  54. }
  55. afterLeave() {
  56. console.log("afterLeave");
  57. }
  58. }
  59. /* eslint-disable react/no-multi-comp */
  60. class Selecter extends React.Component {
  61. constructor(props) {
  62. super(props);
  63. if (props.defaultValue) {
  64. this.state = {
  65. in: props.defaultValue.in,
  66. out: props.defaultValue.out
  67. };
  68. } else {
  69. this.state = {
  70. in: "fadeInLeft",
  71. out: "fadeOutRight"
  72. };
  73. }
  74. }
  75. render() {
  76. return (
  77. <div className="selecter">
  78. <br />
  79. <span>入场动画: </span>
  80. <Select value={this.state.in} onChange={this.onChangeIn}>
  81. {Animate.names.enter.map(i => {
  82. return (
  83. <option key={i} value={i}>
  84. {i}
  85. </option>
  86. );
  87. })}
  88. </Select>
  89. <span> 出场动画: </span>
  90. <Select value={this.state.out} onChange={this.onChangeOut}>
  91. {Animate.names.leave.map(i => {
  92. return (
  93. <option key={i} value={i}>
  94. {i}
  95. </option>
  96. );
  97. })}
  98. </Select>
  99. <br />
  100. <br />
  101. </div>
  102. );
  103. }
  104. onChangeIn = v => {
  105. let animation = { ...this.state, in: v };
  106. this.setState(animation);
  107. this.props.onChange && this.props.onChange(animation);
  108. };
  109. onChangeOut = v => {
  110. let animation = { ...this.state, out: v };
  111. this.setState(animation);
  112. this.props.onChange && this.props.onChange(animation);
  113. };
  114. }
  115. ReactDOM.render(<App />, mountNode);
  1. .demo {
  2. color: #00BCD4;
  3. display: block;
  4. height: 100px;
  5. line-height: 100px;
  6. text-align: center;
  7. font-size: 36px;
  8. background-color: #ddd;
  9. }
  10. .selecter .next-select {
  11. vertical-align: middle;
  12. }

class控制动画

className 用法

Animate 动画 - 图2

查看源码在线预览

  1. import { Animate, Button, Select } from "@icedesign/base";
  2. class App extends React.Component {
  3. state = {
  4. enter: "fadeInLeftBig",
  5. leave: "fadeOutRightBig",
  6. className: ""
  7. };
  8. render() {
  9. return (
  10. <div>
  11. <div className="demo">
  12. <div className={this.state.className}>Aniamte</div>
  13. </div>
  14. <Selecter
  15. defaultValue={{ in: this.state.enter, out: this.state.leave }}
  16. onChange={this.onChange}
  17. />
  18. <Button onClick={this.onClick}>触发</Button>
  19. </div>
  20. );
  21. }
  22. onClick = () => {
  23. this.setState({
  24. className: this.state.enter
  25. });
  26. setTimeout(() => {
  27. this.setState({
  28. className: this.state.leave
  29. });
  30. setTimeout(() => {
  31. this.setState({
  32. className: ""
  33. });
  34. }, 300);
  35. }, 300);
  36. };
  37. onChange = animation => {
  38. this.setState({
  39. enter: animation.in,
  40. leave: animation.out,
  41. className: animation.in
  42. });
  43. setTimeout(() => {
  44. this.onClick();
  45. }, 0);
  46. };
  47. }
  48. /* eslint-disable react/no-multi-comp */
  49. class Selecter extends React.Component {
  50. constructor(props) {
  51. super(props);
  52. if (props.defaultValue) {
  53. this.state = {
  54. in: props.defaultValue.in,
  55. out: props.defaultValue.out
  56. };
  57. } else {
  58. this.state = {
  59. in: "fadeInLeft",
  60. out: "fadeOutRight"
  61. };
  62. }
  63. }
  64. render() {
  65. return (
  66. <div className="selecter">
  67. <br />
  68. <span>入场动画: </span>
  69. <Select value={this.state.in} onChange={this.onChangeIn}>
  70. {Animate.names.enter.map(i => {
  71. return (
  72. <option key={i} value={i}>
  73. {i}
  74. </option>
  75. );
  76. })}
  77. </Select>
  78. <span> 出场动画: </span>
  79. <Select value={this.state.out} onChange={this.onChangeOut}>
  80. {Animate.names.leave.map(i => {
  81. return (
  82. <option key={i} value={i}>
  83. {i}
  84. </option>
  85. );
  86. })}
  87. </Select>
  88. <br />
  89. <br />
  90. </div>
  91. );
  92. }
  93. onChangeIn = v => {
  94. let animation = { ...this.state, in: v };
  95. this.setState(animation);
  96. this.props.onChange && this.props.onChange(animation);
  97. };
  98. onChangeOut = v => {
  99. let animation = { ...this.state, out: v };
  100. this.setState(animation);
  101. this.props.onChange && this.props.onChange(animation);
  102. };
  103. }
  104. ReactDOM.render(<App />, mountNode);
  1. .demo {
  2. color: #00BCD4;
  3. display: block;
  4. height: 100px;
  5. line-height: 100px;
  6. text-align: center;
  7. font-size: 36px;
  8. background-color: #ddd;
  9. }
  10. .selecter .next-select {
  11. vertical-align: middle;
  12. }

入场出场

入场出场

Animate 动画 - 图3

查看源码在线预览

  1. import { Animate } from "@icedesign/base";
  2. const animation = {
  3. enter: "expandInDown",
  4. leave: "expandOutUp"
  5. };
  6. class App extends React.Component {
  7. state = {
  8. items: []
  9. };
  10. render() {
  11. return (
  12. <div className="demo2">
  13. <div className="element">
  14. <div className="addbtn" onClick={this.addItem.bind(this)}>
  15. Add item
  16. </div>
  17. <Animate
  18. singleMode={false}
  19. animation={animation}
  20. afterEnter={this.afterEnter}
  21. afterLeave={this.afterLeave}
  22. >
  23. {this.state.items}
  24. </Animate>
  25. </div>
  26. </div>
  27. );
  28. }
  29. addItem() {
  30. let { items } = this.state,
  31. key = Date.now(),
  32. item = (
  33. <div key={key}>
  34. {" "}
  35. <a href="javascript:;" onClick={this.removeItem.bind(this, key)}>
  36. Remove
  37. </a>
  38. </div>
  39. );
  40. items.push(item);
  41. this.setState({ items });
  42. }
  43. removeItem(key) {
  44. let { items } = this.state,
  45. i,
  46. list = items.slice();
  47. list.forEach((item, index) => {
  48. if (item.key == key) {
  49. i = index;
  50. }
  51. });
  52. if (i > -1) {
  53. list.splice(i, 1);
  54. this.setState({ items: list });
  55. }
  56. }
  57. afterEnter() {
  58. console.log("afterEnter");
  59. }
  60. afterLeave() {
  61. console.log("afterLeave");
  62. }
  63. }
  64. ReactDOM.render(<App />, mountNode);
  1. .demo2 {
  2. color: #4C4C4C;
  3. height: 400px;
  4. background-color: #eee;
  5. margin: 0 auto;
  6. padding-top: 30px;
  7. }
  8. .element{
  9. width: 300px;
  10. margin: 70px auto;
  11. color: #000;
  12. text-align: center;
  13. }
  14. .element span div{
  15. height: 30px;
  16. background-color: #4C4C4C;
  17. border-bottom: 1px solid #eee;
  18. cursor: pointer;
  19. transition: all 1s;
  20. }
  21. .addbtn{
  22. cursor: pointer;
  23. background-color: #2196F3;
  24. height: 40px;
  25. line-height: 40px;
  26. }
  27. .element a {
  28. color: white;
  29. line-height: 30px;
  30. }

相关区块

Animate 动画 - 图4

暂无相关区块