2

我正在尝试在文本输入中显示下一个通知,以通知用户他们的文本已保存,然后将该通知文本淡出。

基本上,当提交相应的文本输入时,我正在尝试复制此操作。

$(this).fadeOut().next().fadeIn();

我想不出任何办法让它淡入,然后淡出,这不是荒谬的迂回。

我也在使用 Redux,并且想使用它,但这不是必需的。任何意见,将不胜感激。

4

2 回答 2

8

您可以使用 CSS 来解决这个问题。这是一个非常简单的例子(不使用 redux)。用JS触发,CSS来样式。

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      show: false
    };
    this.showNotification = this.showNotification.bind(this);
  }
  showNotification() {
    // You can use redux for this.
    this.setState({
      show: true,
    });
    setTimeout(() => {
      this.setState({
        show: false,
      });
    }, 1000);
  }
  render() {
    return (
      <div>
        <button onClick={this.showNotification}>Save</button>
        <Notification show={this.state.show} />
      </div>
    );
  }

}

class Notification extends React.Component {
  render() {
    return <span className={this.props.show ? 'show' : ''}>Saved!</span>
  }
}

ReactDOM.render(<App/>, document.getElementById('View'));
span {
  transition: 300ms all ease;
  opacity: 0;
  will-change: opacity;
}

.show {
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="View"></div>

于 2016-11-05T19:02:56.653 回答
0

我知道反应真的很棒,但不要忘记 jquery:

这有点不那么荒谬:

$('#mydiv').fadeOut(3000)
于 2019-07-01T16:49:47.030 回答