向上滑动反应组件的过渡

时间:2016-10-16 02:21:14

标签: javascript css reactjs css-animations transition

我刚开始在我的React.js项目中使用动画/过渡。在下面的GIF中,当我关闭Message组件时,它会淡出。淡出后,我使用onAnimationEnd隐藏组件。

我想要发生的是当Message消失时,下面的组件会向上滑动。我在概念上不确定如何通过CSS动画/转换或通过React特定方式实现此目的。

enter image description here

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);

        }

    }
}

2 个答案:

答案 0 :(得分:2)

我建议您使用css transitions

将一个名为open的类附加到Message组件的根目录。 onAnimationEnd删除课程open。现在,使用height为该类设置动画。

伪代码。

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

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

答案 1 :(得分:1)

有一个名为npm的{​​{1}}模块可以解决问题。

react-collapse

结果如下:

enter image description here