Animate 动画

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

安装方法

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

开发指南

何时使用

需要自定义动效

API

Animate

参数说明类型默认值
animation动画 classNameString/Object-
animationAppear子元素第一次挂载时是否执行动画Booleantrue
component包裹子元素的标签any'div'
singleMode是否只有单个子元素,如果有多个子元素,请设置为 falseBooleantrue
children子元素ReactElement/Array<ReactElement>-
beforeAppear执行第一次挂载动画前触发的回调函数签名:Function() => voidFunction() => {}
onAppear执行第一次挂载动画,添加 xxx-appear-active 类名后触发的回调函数签名:Function(node: HTMLElement) => void参数:node: {HTMLElement} 执行动画的 dom 元素Function() => {}
afterAppear执行完第一次挂载动画后触发的函数签名:Function(node: HTMLElement) => void参数:node: {HTMLElement} 执行动画的 dom 元素Function() => {}
beforeEnter执行进场动画前触发的回调函数签名:Function(node: HTMLElement) => void参数:node: {HTMLElement} 执行动画的 dom 元素Function() => {}
onEnter执行进场动画,添加 xxx-enter-active 类名后触发的回调函数签名:Function(node: HTMLElement) => void参数:node: {HTMLElement} 执行动画的 dom 元素Function() => {}
afterEnter执行完进场动画后触发的回调函数签名:Function(node: HTMLElement) => void参数:node: {HTMLElement} 执行动画的 dom 元素Function() => {}
beforeLeave执行离场动画前触发的回调函数签名:Function(node: HTMLElement) => void参数:node: {HTMLElement} 执行动画的 dom 元素Function() => {}
onLeave执行离场动画,添加 xxx-leave-active 类名后触发的回调函数签名:Function(node: HTMLElement) => void参数:node: {HTMLElement} 执行动画的 dom 元素Function() => {}
afterLeave执行完离场动画后触发的回调函数签名:Function(node: HTMLElement) => void参数:node: {HTMLElement} 执行动画的 dom 元素Function() => {}

Animation List

InOut
fadeInfadeOut
fadeInDownfadeOutDown
fadeInLeftfadeOutLeft
fadeInRightfadeOutRight
fadeInUpfadeOutUp
zoomInzoomOut
expandInDownexpandOutUp
expandInUpexpandOutDown
pulse

代码示例

基本

展示单个子元素的进场离场动画。

Animate 动画 - 图1

查看源码在线预览

  1. import { Animate } from '@alifd/next';
  2. class Demo extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = { visible: true };
  6. this.handleToggle = this.handleToggle.bind(this);
  7. }
  8. handleToggle() {
  9. this.setState({
  10. visible: !this.state.visible
  11. });
  12. }
  13. render() {
  14. return (
  15. <div>
  16. <button onClick={this.handleToggle}>Toggle visible</button>
  17. <Animate animation={{
  18. enter: 'my-zoom-in',
  19. leave: 'my-zoom-out'
  20. }}>
  21. {this.state.visible ?
  22. <div className="basic-demo">Next Animate</div> :
  23. null}
  24. </Animate>
  25. </div>
  26. );
  27. }
  28. }
  29. ReactDOM.render(<Demo />, mountNode);
  1. .basic-demo {
  2. font-size: 96px;
  3. text-align: center;
  4. }
  5. .my-zoom-in {
  6. opacity: 0;
  7. }
  8. .my-zoom-in-active {
  9. animation: my-zoom-in 500ms linear;
  10. }
  11. .my-zoom-out {
  12. opacity: 1;
  13. }
  14. .my-zoom-out-active {
  15. animation: my-zoom-out 500ms linear;
  16. }
  17. @keyframes my-zoom-in {
  18. from {
  19. opacity: 0;
  20. transform: scale3d(.3, .3, .3);
  21. }
  22. 50% {
  23. opacity: 1;
  24. }
  25. to {
  26. opacity: 1;
  27. }
  28. }
  29. @keyframes my-zoom-out {
  30. from {
  31. opacity: 1;
  32. }
  33. 50% {
  34. opacity: 0;
  35. transform: scale3d(.3, .3, .3);
  36. }
  37. to {
  38. opacity: 0;
  39. }
  40. }

多个子元素动画

展示多个子元素的进场离场动画。

Animate 动画 - 图2

查看源码在线预览

  1. import { Animate } from '@alifd/next';
  2. class TodoList extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = { items: ['hello', 'world', 'click', 'me'] };
  6. }
  7. handleAdd() {
  8. this.setState({
  9. items: [
  10. ...this.state.items,
  11. // eslint-disable-next-line
  12. prompt('Enter some text')
  13. ]
  14. });
  15. }
  16. handleRemove(i) {
  17. const newItems = this.state.items.slice();
  18. newItems.splice(i, 1);
  19. this.setState({ items: newItems });
  20. }
  21. render() {
  22. return (
  23. <div className="totolist-container">
  24. <Animate animationAppear animation="fade" className="todo-list" singleMode={false}
  25. beforeAppear={() => console.log('before appear')}
  26. onAppear={() => console.log('appear')}
  27. afterAppear={() => console.log('after appear')}
  28. beforeEnter={() => console.log('before enter')}
  29. onEnter={() => console.log('enter')}
  30. afterEnter={() => console.log('after enter')}
  31. beforeLeave={() => console.log('before leave')}
  32. onLeave={() => console.log('leave')}
  33. afterLeave={() => console.log('after leave')}>
  34. {this.state.items.map((item, i) => (
  35. <div key={item}>
  36. {item}
  37. <button onClick={() => this.handleRemove(i)}>
  38. &times;
  39. </button>
  40. </div>
  41. ))}
  42. </Animate>
  43. <button onClick={() => this.handleAdd()}>Add Item</button>
  44. </div>
  45. );
  46. }
  47. }
  48. ReactDOM.render(<TodoList />, mountNode);
  1. .fade-appear {
  2. opacity: 0.01;
  3. }
  4. .fade-appear.fade-appear-active {
  5. opacity: 1;
  6. transition: opacity 1000ms ease-in;
  7. }
  8. .fade-enter {
  9. opacity: 0.01;
  10. }
  11. .fade-enter.fade-enter-active {
  12. opacity: 1;
  13. transition: opacity 1000ms ease-in;
  14. }
  15. .fade-leave {
  16. opacity: 1;
  17. }
  18. .fade-leave.fade-leave-active {
  19. opacity: 0.01;
  20. transition: opacity 800ms ease-in;
  21. }
  22. .totolist-container {
  23. padding: 20px;
  24. border: 1px solid #ccc;
  25. width: 200px;
  26. border-radius: 4px;
  27. }
  28. .todo-list > * {
  29. margin: 5px 0;
  30. padding: 5px 0;
  31. border-bottom: 1px solid #ccc;
  32. }
  33. .totolist-container > button {
  34. margin-top: 20px;
  35. }

展开收起动画

展示单个子元素的展开收起动画。

Animate 动画 - 图3

查看源码在线预览

  1. import { Animate } from '@alifd/next';
  2. class Demo extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = { expand: true };
  6. ['beforeEnter', 'onEnter', 'afterEnter', 'beforeLeave', 'onLeave', 'afterLeave', 'handleToggle'].forEach(method => {
  7. this[method] = this[method].bind(this);
  8. });
  9. }
  10. handleToggle() {
  11. this.setState({
  12. expand: !this.state.expand
  13. });
  14. }
  15. beforeEnter(node) {
  16. this.height = node.offsetHeight;
  17. node.style.height = '0px';
  18. }
  19. onEnter(node) {
  20. node.style.height = `${this.height}px`;
  21. }
  22. afterEnter(node) {
  23. this.height = null;
  24. node.style.height = null;
  25. }
  26. beforeLeave(node) {
  27. node.style.height = `${this.height}px`;
  28. }
  29. onLeave(node) {
  30. node.style.height = '0px';
  31. }
  32. afterLeave(node) {
  33. node.style.height = null;
  34. }
  35. render() {
  36. return (
  37. <div>
  38. <button onClick={this.handleToggle}>Toggle expand</button>
  39. <Animate animation="expand"
  40. beforeEnter={this.beforeEnter}
  41. onEnter={this.onEnter}
  42. afterEnter={this.afterEnter}
  43. beforeLeave={this.beforeLeave}
  44. onLeave={this.onLeave}
  45. afterLeave={this.afterLeave}>
  46. {this.state.expand ?
  47. <div className="notice"></div> :
  48. null}
  49. </Animate>
  50. </div>
  51. );
  52. }
  53. }
  54. ReactDOM.render(<Demo />, mountNode);
  1. .expand-enter {
  2. overflow: hidden;
  3. }
  4. .expand-enter-active {
  5. transition: height 0.3s ease-out;
  6. }
  7. .expand-leave {
  8. overflow: hidden;
  9. }
  10. .expand-leave-active {
  11. transition: height 0.3s ease-out;
  12. }
  13. .notice {
  14. width: 200px;
  15. height: 100px;
  16. margin-top: 20px;
  17. border: 1px solid #ccc;
  18. }

相关区块

Animate 动画 - 图4

暂无相关区块