react 动画 react-motion

柔情只为你懂 2022-12-03 11:26 318阅读 0赞

安装包

npm install react-motion —save

简单示例

  1. import React from 'react'
  2. import IComponent from 'BaseCMSManage/Components/IETemplateComponents/IEAnimation/IComponent'
  3. import { Motion, spring, presets } from 'react-motion'
  4. class IEAnimation extends IComponent {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. value: 0
  9. }
  10. }
  11. componentDidMount() {
  12. this.setState({ value: 100 });
  13. }
  14. render() {
  15. // presets 是预设的几种动画类型
  16. // 动画执行过程
  17. // 初始化时 x = 0,
  18. // 当我们 setState 时,x = 100
  19. // 这时将产生一个 x 从 0 到 100 的一个变化,如(0, 20, 40, 60, 80, 100),如何变化取决于我们的预设 presets
  20. // 每次变化将会调用我们的 动画组件 生成组件
  21. return (<div>
  22. <Motion style={ { x: spring(this.state.value, presets.wobbly) }}>
  23. { /* 动画组件 */}
  24. { interpolatingStyle => {
  25. return (
  26. <div style={ { left: `${ interpolatingStyle.x - 100}%`, position: 'relative' }}>
  27. {
  28. this.props.children
  29. }
  30. </div>
  31. )
  32. }}
  33. </Motion>
  34. </div>);
  35. }
  36. }

联动动画StaggeredMotion组件

联动动画执行过程n次变化如下:
1.组件1[x=10],组件2[x=0],组件3[x=0]
2.组件1[x=20],组件2[x=10], 组件3[x=0]
3.组件1[x=30],组件2[x=20],组件3[x=10]

示例:

  1. import React from 'react'
  2. import IComponent from 'BaseCMSManage/Components/IETemplateComponents/IEAnimation/IComponent'
  3. import { Motion, spring, presets, StaggeredMotion } from 'react-motion'
  4. class IEAnimation extends IComponent {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. value: 0
  9. }
  10. }
  11. componentDidMount() {
  12. this.setState({ value: 100 });
  13. }
  14. render() {
  15. let childrens = [
  16. { childVal: this.state.value},
  17. { childVal: this.state.value},
  18. { childVal: this.state.value}
  19. ]
  20. return (<div>
  21. <StaggeredMotion
  22. defaultStyles={ childrens}
  23. styles={ prevInterpolatedStyles => {
  24. // 这一步很重要,prevInterpolatedStyles 是上一次变化的 styles
  25. // 而我们要返回的是这一次变化的 styles
  26. // 我们这一次变化的 styles,要基于上一次变化
  27. // 也就是说这一次的 styles[1] 要等于上一次的 prevInterpolatedStyles[0],同理 2->1,3->2 ...
  28. return prevInterpolatedStyles.map((_, i) => {
  29. return i === 0
  30. ? { childVal: spring(this.state.value)}
  31. : { childVal: spring(prevInterpolatedStyles[i - 1].childVal, presets.wobbly)}
  32. })
  33. }}
  34. >
  35. { interpolatingStyles => {
  36. return <>
  37. {
  38. interpolatingStyles.map((e, i)=>(
  39. <div style={ { left: `${ e.childVal - 100}%`, position: 'relative' }}></div>
  40. ))
  41. }
  42. </>
  43. }}
  44. </StaggeredMotion>
  45. </div>);
  46. }
  47. }

挂载卸载动画TransitionMotion

没试过,直接使用官方示例:

  1. import createReactClass from 'create-react-class';
  2. const Demo = createReactClass({
  3. getInitialState() {
  4. return {
  5. items: [{ key: 'a', size: 10}, { key: 'b', size: 20}, { key: 'c', size: 30}],
  6. };
  7. },
  8. componentDidMount() {
  9. this.setState({
  10. items: [{ key: 'a', size: 10}, { key: 'b', size: 20}], // remove c.
  11. });
  12. },
  13. willLeave() {
  14. // triggered when c's gone. Keeping c until its width/height reach 0.
  15. return { width: spring(0), height: spring(0)};
  16. },
  17. render() {
  18. return (
  19. <TransitionMotion
  20. willLeave={ this.willLeave}
  21. styles={ this.state.items.map(item => ({
  22. key: item.key,
  23. style: { width: item.size, height: item.size},
  24. }))}>
  25. { interpolatedStyles =>
  26. // first render: a, b, c. Second: still a, b, c! Only last one's a, b.
  27. <div>
  28. { interpolatedStyles.map(config => {
  29. return <div key={ config.key} style={ { ...config.style, border: '1px solid'}} />
  30. })}
  31. </div>
  32. }
  33. </TransitionMotion>
  34. );
  35. },
  36. });

发表评论

表情:
评论列表 (有 0 条评论,318人围观)

还没有评论,来说两句吧...

相关阅读