One possible method to achieve this is as follows:
$('.section-modal .profile, .section-modal .overlay .modal .title').click(function() {
var $overlay = $('.overlay');
if ($overlay.hasClass('is-active')) {
$overlay.addClass('hidden').on('animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd', function(){
$overlay.removeClass('is-active hidden');
})
} else {
$overlay.addClass('is-active').removeClass('hidden');
}
});
.section-modal {
width: 700px;
height: 700px;
margin: 0 auto;
position: relative;
overflow: hidden;
}
.section-modal .profile {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
background: rgba(0, 0, 0, 0.1);
background: white;
padding: 15px 30px;
border-radius: 4px;
box-shadow: 0px 23px 30px -20px rgba(0, 0, 0, 0.4);
-webkit-transition: all 200ms ease-in-out;
transition: all 200ms ease-in-out;
}
.section-modal .profile .name {
font-size: 24px;
margin-bottom: 8px;
}
.section-modal .profile .meta {
color: rgba(0, 0, 0, 0.4);
}
.section-modal .profile img {
width: 80px;
height: 80px;
border-radius: 50%;
margin-right: 20px;
}
.section-modal .overlay {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.7);
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
display: none;
}
<section class="section-modal">
<div class="profile"><img src="http://www.fillmurray.com/130/130"/>
<div class="text">
<div class="name">Bill Murray</div>
<div class="meta">Click me!</div>
</div>
</div>
<div class="overlay">
<div class="modal">
<div class="title">CLOSE MODAL</div>
<div class="body">
<div style="background-image: url(http://www.fillmurray.com/180/180)" class="img"></div>
<div class="text">
<p>Bill Murray loves you, and sends his most sincere regards.</p>
<p>He also asks that you simply keep on hacking.</p>
</div>
</div>
</div>
</div>
</section>
The key modification is implemented here by applying a reverse animation when the active class becomes hidden:
.section-modal .overlay.is-active.hidden {
-webkit-animation: overlayAnim 2s ease-in-out reverse;
animation: overlayAnim 2s ease-in-out reverse;
}
.section-modal .overlay.is-active.hidden .modal {
-webkit-animation: modalAnim 2s ease-in-out reverse;
animation: modalAnim 2s ease-in-out reverse;
}
Additionally, I have incorporated a check for an active class, instantly adding a hidden class if true, and waiting for the animation to complete before removing both:
if ($overlay.hasClass('is-active')) {
$overlay.addClass('hidden').on('animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd', function(){
$overlay.removeClass('is-active hidden');
})
} else {
$overlay.addClass('is-active');
}
This approach should effectively fulfill the intended functionality.