フルコード:
モーダルボタンをクリックしてアニメートします。しかし、私はそれを閉じるときにcomponents/Modal/index.css
でアニメーションを反転させたいと思っています。フェードアウトするモーダル時間を与えるためにsetTimeout
を実装しましたが、動作しません。それはどうすればうまくいくのですか?アニメーションの逆のクラスを動的に追加するに反応します
モーダル/ index.js:
import React from 'react';
import Question from './Question';
import Loading from './Loading';
import Success from './Success';
import './index.css';
class Modal extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: false,
success: false,
reverse: false,
};
}
removeUser =() => {
this.setState({ loading: true });
// simulate async loading action
setTimeout(
() =>
this.setState({
loading: false,
success: true,
}),
2000,
);
//simulate promise (function above then this below)
setTimeout(() => this.props.removeUser(this.props.data), 2001);
};
closeModal =() => {
this.setState({ reverse: true });
setTimeout(() => {
this.props.closeModal();
this.setState({
reverse: false,
success: false,
});
}, 3000);
};
render() {
const { id, name } = this.props.data;
const { loading, success } = this.state;
// The gray background
const backdropStyle = {
position: 'fixed',
top: 0,
bottom: 0,
left: 0,
right: 0,
backgroundColor: 'rgba(0,0,0,0.3)',
padding: 50,
};
// The modal "window"
const modalStyle = {
backgroundColor: '#fff',
textAlign: 'center',
borderRadius: 3,
maxWidth: 360,
minHeight: 130,
margin: '0 auto',
};
if (this.props.displayModal) {
return (
<div className={'backdrop ' + (this.state.reverse ? 'reverse' : '')} style={backdropStyle}>
<div className={'modal ' + (this.state.reverse ? 'reverse' : '')} style={modalStyle}>
{!loading &&
!success && (
<Question
id={id}
name={name}
removeUser={this.removeUser}
closeModal={this.closeModal}
/>
)}
{loading && !success && <Loading />}
{!loading && success && <Success closeModal={this.closeModal} />}
</div>
</div>
);
}
return null;
}
}
export default Modal;
モーダル/ index.css:
@keyframes modalFade {
from {
transform: translateY(-50%);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
.modal {
animation-name: modalFade;
animation-duration: 0.3s;
}
@keyframes backdropFade {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.backdrop {
animation-name: backdropFade;
animation-duration: 0.3s;
}
.reverse {
animation-direction: reverse;
}
ただ、クラス名に追加取得された「逆」というデベロッパーツールに気づいcodesandbox –
@artgb内のコードに行わ、完全なコードを提供するので、それはCSSであれば、私は思っていたしてくださいそれは仕事が必要ですか?私も学んでいます。 –
に質問 – anand