我刚开始在我的React.js
项目中使用动画/过渡。在下面的GIF中,当我关闭Message
组件时,它会淡出。淡出后,我使用onAnimationEnd
隐藏组件。
我想要发生的是当Message
消失时,下面的组件会向上滑动。我在概念上不确定如何通过CSS动画/转换或通过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);
}
}
}
答案 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)