React 组件的上滑过渡

Slide Up Transition of React Component

我刚开始在我的 React.js 项目中使用 animations/transition。在下面的 GIF 中,当我关闭 Message 组件时,它会淡出。消失后,我使用 onAnimationEnd.

隐藏组件

我想要发生的是当 Message 消失时让下面的组件向上滑动。我在概念上不确定如何通过 CSS animation/transition 或通过特定于 React 的方式来完成此操作。

Message.js

import React, {PropTypes, Component} from 'react';
import MessageTypes from '../constants/MessageTypes';

export default class Message extends Component {

    constructor(props, context) {
        super(props, context);
        this.handleClick = this.handleClick.bind(this);
        this.onAnimationEnd = this.onAnimationEnd.bind(this);
        this.state = {
            animate: false,
            hide: false
        }
    }

    handleClick(e) {
        e.preventDefault();
        this.setState({animate: true});
    }

    onAnimationEnd() {
        console.log('fdsfd');
        this.setState({hide: true});
    }

    render() {
        return (
            <div ref="message" onAnimationEnd={this.onAnimationEnd} className={`card message ${this.state.hide ? 'hide open' : ''} ${this.state.animate ? 'message-transition-fade-out' : ''}`}>
                <span className={`icon icon-${this.iconLevel()}`} style={{color: `#${this.iconColor()}`}}></span>
                <p>
                    <strong>{this.props.title}</strong><br />
                    <span dangerouslySetInnerHTML={{ __html: this.props.message }} />
                </p>
                <a href="#" onClick={this.handleClick}>
                    <span className="icon icon-close"></span>
                </a>
            </div>  
        );
    }

}

Message.scss

.message {

    max-height: 150px;
    transition: height 2s ease;

    &.open {
        height: 0; //define your height or use max-height
    }

    &-transition {

        &-fade-out {

            @include animation(0s, .3s, fadeout);

        }

    }
}

我建议您使用 css transitions

将名为 open 的 class 附加到 Message 组件的根。 onAnimationEnd 删除 class open。现在,使用 height 为 class.

设置动画

伪代码。

.message {
 height: 0px;
 transition: height 0.3s ease;
}

.message.open {
  height: 100px; //define your height or use max-height  
} 

有一个名为 react-collapsenpm 模块可以解决问题。

<Collapse isOpened={this.state.isOpened} keepCollapsedContent={true}>
     //content here
</Collapse>

结果如下: