【问题标题】:React Transition Animation is not working as expectedReact 过渡动画未按预期工作
【发布时间】:2018-04-14 17:28:05
【问题描述】:

我在尝试为注册表单设置动画时遇到了 reactJS 问题。动画应该在表单出现时从顶部淡入,然后当用户单击注册时它应该淡出。这是代码

import { TransitionGroup, CSSTransition} from 'react-transition-group';
...
import "./App.css";

class App extends Component {

  state = {
    mounted: false
  }

  constructor(props) {
    super(props);
    this.state = { mounted: false };
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  getInitialState() {
    return { mounted: false };
  }

  componentDidMount() {
    this.setState({ mounted: true });
  }

  handleSubmit(e) {
    this.setState({ mounted: false });
    e.preventDefault();
  }

  render() {
    var child;
    if(this.state.mounted) {
        child = (<Modal onSubmit={this.handleSubmit} />);
    } else {
      child = (<div />);
    }

    return (
      <div className="App">
        <TransitionGroup>
          <CSSTransition
            in={this.state.mounted}
            className="example"
            timeout={{ enter: 500, exit: 300}}
          >
          {child}
          </CSSTransition>
        </TransitionGroup>
      </div>
    );
  }
}

export default App;

这是 CSS App.css:

.example-enter {
  margin-top: 30px;
  opacity: .01;
}
.example-enter.example-enter-active {
  margin-top: 0px;
  opacity: 1;
  transition: opacity 1000ms ease, margin .5s ease;
}

.example-exit {
  margin-top: 0px;
  opacity: 1;
}
.example-exit.example-exit-active {
  margin-top: -30px;
  opacity: .01;
  transition: opacity .3s ease, margin .5s ease;
}

谁能指出问题出在哪里。谢谢

【问题讨论】:

    标签: css reactjs animation


    【解决方案1】:

    我在“react-transition-group”文档中发现 CSSTransition 元素具有名为“classNames”的属性,也许你会有机会

    【讨论】:

    • 这似乎没有直接解决问题。
    • 在他的问题中,他使用了 className,所以我认为它导致了问题
    • 其实这只是一个错字。我写了“className”,而它应该是“classNames”
    猜你喜欢
    • 2014-03-30
    • 2017-09-17
    • 2021-10-19
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 2015-12-20
    相关资源
    最近更新 更多