【问题标题】:react: fade-in/slide-in animation when render component反应:渲染组件时的淡入/滑入动画
【发布时间】:2020-07-03 01:03:20
【问题描述】:

我是 React 的新手。我制作了一个带有按钮和图像网址列表的小应用程序。单击按钮时,图像 url 将添加到列表中。我使用标准.map 函数呈现图像网址列表。

我想在图像显示时制作一个快速的ui动画效果:淡入和从左滑入的组合。我尝试了 Velocity.js 并找到了 velocity-react 包装器。但我无法理解如何使用它。 “标准”velocity-animate 库也是如此。

什么是最好的? velocity-reactvelocity-animate 还是别的什么? 我该怎么做?

JSX

<div className="row">
  {
    this.state.images.map( (image, index) => { return this.renderThumb(image, index); } )
  }
</div>

renderThumb 函数

renderThumb(image, index) {
  return (
    <div ref="tweetImage" key={`image-${index}`} className="col-xs-3 tweetImage">
      <img className="img-thumbnail" src={image} alt="my pic"/>
    </div>
  );
}

速度反应

我尝试像这样将 &lt;img&gt; 动画不透明度从 0 包装到 1(从 docs 复制):

<VelocityComponent animation={{ opacity: 1 }} duration={ 500 }>
  <img className="img-thumbnail" src={image} alt="my pic"/>
</VelocityComponent

我不断收到此错误:

警告:React.createElement: type is invalid -- 期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:对象


ReactCSSTransitionGroup 也没有运气(如下面的建议)。显示图像但没有动画:

renderThumb(image, index) {
  return (
    <div ref="tweetImage" key={`image-${index}`} className="col-xs-3">
      <ReactCSSTransitionGroup
        transitionName="example">
          <img className="img-thumbnail" src={image} alt="Ole Frank Jensen"/>
      </ReactCSSTransitionGroup>
    </div>
  );
}

已解决:

我将 &lt;ReactCSSTransitionGroup transitionName="example"&gt; 移到了衰落组件之外,瞧 :-)

渲染()

<div className="row">
  <ReactCSSTransitionGroup transitionName="example">
    {
      this.state.images.map( (image, index) => { return this.renderThumb(image, index); } )
    }
  </ReactCSSTransitionGroup>
</div>

renderThumb()

renderThumb(image, index) {
  return (
    <div key={`image-${index}`} className="col-xs-3">
      <img className="img-thumbnail" src={image} alt="Ole Frank Jensen"/>
    </div>
  );
}

【问题讨论】:

  • 注意this.state.images.map( (image, index) =&gt; { return this.renderThumb(image, index); } )可以写成this.state.images.map( this.renderThumb )

标签: reactjs animation css-animations velocity.js reactcsstransitiongroup


【解决方案1】:

你可以使用 react 提供的 CSSTransitionGroup。

https://facebook.github.io/react/docs/animation.html

文档中的简单待办事项示例

class TodoList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {items: ['hello', 'world', 'click', 'me']};
    this.handleAdd = this.handleAdd.bind(this);
  }

  handleAdd() {
    const newItems = this.state.items.concat([
      prompt('Enter some text')
    ]);
    this.setState({items: newItems});
  }

  handleRemove(i) {
    let newItems = this.state.items.slice();
    newItems.splice(i, 1);
    this.setState({items: newItems});
  }

  render() {
    const items = this.state.items.map((item, i) => (
      <div key={item} onClick={() => this.handleRemove(i)}>
        {item}
      </div>
    ));

    return (
      <div>
        <button onClick={this.handleAdd}>Add Item</button>
        <ReactCSSTransitionGroup
          transitionName="example"
          transitionEnterTimeout={500}
          transitionLeaveTimeout={300}>
          {items}
        </ReactCSSTransitionGroup>
      </div>
    );
  }
}

【讨论】:

    【解决方案2】:

    我建议使用React CSS Transistion Group 模块。它为简单动画提供了高级动画包装器。


    编辑:

    链接将永久重定向到Animation Add-Ons

    ReactTransitionGroupReactCSSTransitionGroup 已移至由社区维护的 react-transition-group 包。


    来自文档

     .example-enter {
      opacity: 0.01;
    }
    
    .example-enter.example-enter-active {
      opacity: 1;
      transition: opacity 500ms ease-in;
    }
    
    .example-leave {
      opacity: 1;
    }
    
    .example-leave.example-leave-active {
      opacity: 0.01;
      transition: opacity 300ms ease-in;
    }
    

    example 将是您要绘制的组件的转换名称。而-enter,enter-active,-leave,-leave-active 代表动画循环的相应刻度。这些将由 React 在内部作为类名添加到项目中。

    您可以使用它们来达到预期的效果。一个小演示here

    PS:不确定这是否优于 Velocity.js,没有使用过。

    【讨论】:

    • 对不起,我尝试了你的代码并将'example'作为类名添加到图像中并在 中包装图像,但它不起作用......
    • 对不起,这是我的错误。应该是transistionName。也更新了小提琴,以满足您的要求,使用 lorem 像素作为图像。让我知道是否可行。:)
    猜你喜欢
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2021-09-15
    • 2014-09-21
    • 2019-11-02
    相关资源
    最近更新 更多