react-transition-group CSSTransition幻灯片在渲染中

时间:2019-05-24 18:59:26

标签: css reactjs react-transition-group

我试图在React中有条件地渲染一个组件,并在渲染时滑动它

软件包:

"react": "^16.8.6",
"react-transition-group": "^4.0.1",

代码段:

{ expanded && (
    <CSSTransition in={expanded} timeout={500} classNames="slide">
        <div className="expandedDiv"></div>
    </CSSTransition>
)}

css:

.slide-enter {
    transform: translateX(-100%);
    transition: .3s linear;
}
.slide-enter-active {
    transform: translateX(0%);
}
.slide-exit {
    transform: translateX(0%);
    transition: .3s linear;
}
.slide-exit-active {
    transform: translateX(-100%);
}

结构:

+------------------------------+
|      |          |  header    |
|      |          |____________+
|      |          |  content   |
|      |          |            |
|      |          |            |
| nav  | expanded |            |
|      |          |            |
|      |          |            |
|      |          |            |
+------------------------------+

展开后,应为true,否则为false,这是代码段中的expandedDiv

div仅弹出而没有动画。预期行为是从左到右的线性过渡

谢谢

2 个答案:

答案 0 :(得分:0)

我需要多看一些代码,以准确了解您要执行的操作。但是,如果我做对了,请尝试这样做:

<CSSTransition
  in={expanded}
  appear={true}
  key = {this.props.location.key}
  timeout={500}
  classNames="slide">
            <div className='expandedDiv'> </div>
</CSSTransition>

答案 1 :(得分:0)

               <CSSTransition
                    in={expanded}
                    timeout={300}
                    classNames="slide"
                    unmountOnExit
                    onEnter={() => this.toggleExpanded(true)}
                    onExited={() => this.toggleExpanded(false)}
                >
                    <div className="expandedDiv">
                    </div>
                </CSSTransition>
            </div>