首页 > 解决方案 > CSSTransition 持续时间不起作用

问题描述

我正在尝试使用CSSTransitionduration来实现向下滑动动画,但它似乎transition不起作用。

密码笔

组件.jsx

let { CSSTransition, TransitionGroup } = ReactTransitionGroup

class Example extends React.Component {

    state = {
    show: false,
  };

    render() {
        return (
          <div>
            <button onClick={() => this.setState({
          show: true })}>Show</button>
          {this.state.show && (
            <CSSTransition in={this.state.show} timeout={2000} classNames="birthday-input" appear>
              <div className="form-group birthday-input">
                <h1>HEADER</h1>
              </div>
            </CSSTransition> 
          )}
            </div>
        );
    }
}

ReactDOM.render(
  <Example />,
  document.getElementById('root')
);

组件.scss

.birthday-input-enter {
  max-height: 0;
}
.birthday-input-enter-active {
  -webkit-transition: max-height 1s ease;
  max-height: 1000px;
}
.birthday-input-exit {
  max-height: 1000px;
}
.birthday-input-exit-active {
  max-height: 0;
  -webkit-transition: max-height 1s ease;
}

标签: reactjscsscss-transitionscss-animationsreactcsstransitiongroup

解决方案


来自库文档:当inprop 切换为 true 时,组件将获得example-enterCSS 类,并example-enter-active在下一个刻度中添加 CSS 类。这是基于classNames道具的约定。

首先,在您的情况下,in永远不会评估为 true 因为showBirthInputis undefined

其次,它必须与 JS 评估有关,因为如果我取出 JS 评估并要求CSSTransition对其进行渲染,show则可以完美运行。

这是基于您的代码的工作示例: https ://stackblitz.com/edit/react-pshw1h?file=index.js


推荐阅读