Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Animation: Remove class with gently transition

In this code, the modal is called with a gently transition (CSS3). When I clicked in "Clode Modal" the class .is-active is removed. But, it is removed instantaneously. I would like to do the reverse effect.

When modal is opened, it appears from left to right. When I close the modal I want him to do the reverse effect. Where he is (center) to the left.

How can I do this?

$('.section-modal .profile, .section-modal .overlay .modal .title').click(function(){
  $('.overlay').toggleClass('is-active');
});
.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-modal .overlay .modal {
  width: 450px;
  box-shadow: 0px 23px 30px -20px rgba(0, 0, 0, 0.4);
}
.section-modal .overlay .modal .title {
  background: #0097A7;
  color: white;
  border-radius: 4px 4px 0px 0px;
  text-align: center;
  line-height: 48px;
  font-weight: 700;
}
.section-modal .overlay .modal .body {
  background: white;
  border-radius: 0px 0px 4px 4px;
  line-height: 20px;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: stretch;
  -webkit-align-items: stretch;
      -ms-flex-align: stretch;
          align-items: stretch;
}
.section-modal .overlay .modal .body .text {
  padding: 30px;
}
.section-modal .overlay .modal .body .text p {
  margin-bottom: 20px;
}
.section-modal .overlay .modal .body .img {
  height: 180px;
  width: 140px;
  border-bottom-left-radius: 4px;
  -webkit-flex-shrink: 0;
      -ms-flex-negative: 0;
          flex-shrink: 0;
  background-size: cover;
  background-position: center;
}
.section-modal .overlay.is-active {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-animation: overlayAnim 5s ease-in-out alternate;
          animation: overlayAnim 5s ease-in-out alternate;
}
.section-modal .overlay.is-active .modal {
  -webkit-animation: modalAnim 5s ease-in-out alternate;
          animation: modalAnim 5s ease-in-out alternate;
}

@keyframes overlayAnim {
  0%, 100% {
    background-color: transparent;
  }
  15%, 85% {
    background-color: rgba(0, 0, 0, 0.3);
  }
}
@keyframes modalAnim {
  0% {
    -webkit-transform: translateX(-200%) rotate(-90deg);
            transform: translateX(-200%) rotate(-90deg);
    opacity: 0;
  }
  10% {
    -webkit-transform: translateX(4%) rotate(4deg);
            transform: translateX(4%) rotate(4deg);
  }
  15%, 100% {
    -webkit-transform: translateX(0%);
            transform: translateX(0%);
    opacity: 1;
  }

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
like image 700
Zkk Avatar asked Sep 28 '16 19:09

Zkk


2 Answers

You need a delay related to the time required for the output effect. You can do this with only css transition-delay.

I created a CSS animation for entry effect (modalAnimIn) and the output effect (modalAnimOut). And I had to make a small change in js to call the modalAnimOut in output, changing the class instead of removing.

Note: It's not necessary to create an animation for overley. Use transition that is simpler

  $('.section-modal .profile, .section-modal .overlay .modal .title').click(function(){
    $('.overlay').toggleClass('inactive is-active');
  });
  .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: -100%;
    width: 100%;
    height: 100%;
    background: transparent;
    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;
    -webkit-transition: left 0s 1s, background 1s 0s;
            transition: left 0s 1s, background 1s 0s;
  }
  .section-modal .overlay .modal {
    width: 450px;
    box-shadow: 0px 23px 30px -20px rgba(0, 0, 0, 0.4);
  }
  .section-modal .overlay .modal .title {
    background: #0097A7;
    color: white;
    border-radius: 4px 4px 0px 0px;
    text-align: center;
    line-height: 48px;
    font-weight: 700;
    cursor: pointer;
  }
  .section-modal .overlay .modal .body {
    background: white;
    border-radius: 0px 0px 4px 4px;
    line-height: 20px;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: stretch;
    -webkit-align-items: stretch;
        -ms-flex-align: stretch;
            align-items: stretch;
  }
  .section-modal .overlay .modal .body .text {
    padding: 30px;
  }
  .section-modal .overlay .modal .body .text p {
    margin-bottom: 20px;
  }
  .section-modal .overlay .modal .body .img {
    height: 180px;
    width: 140px;
    border-bottom-left-radius: 4px;
    -webkit-flex-shrink: 0;
        -ms-flex-negative: 0;
            flex-shrink: 0;
    background-size: cover;
    background-position: center;
  }
  .section-modal .overlay.is-active {
    background: rgba(0, 0 ,0 ,0.7);
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    left: 0;
    -webkit-transition: background 1s 0s, left 0s 0s;
            transition: background 1s 0s, left 0s 0s;
  }
  .section-modal .overlay.is-active .modal {
    -webkit-animation: modalAnimIn 1s;
            animation: modalAnimIn 1s;
  }
  .section-modal .overlay.inactive .modal {
    -webkit-animation: modalAnimOut 1s;
            animation: modalAnimOut 1s;
  }

  @keyframes modalAnimIn {
    0% {
      -webkit-transform: translateX(-200%) rotate(-90deg);
              transform: translateX(-200%) rotate(-90deg);
      opacity: 0;
    }
    50% {
      -webkit-transform: translateX(4%) rotate(4deg);
              transform: translateX(4%) rotate(4deg);
    }
    100% {
      -webkit-transform: translateX(0%);
              transform: translateX(0%);
      opacity: 1;
    }
  }
  @keyframes modalAnimOut {
    0% {
      -webkit-transform: translateX(0%);
              transform: translateX(0%);
      opacity: 1;
    }
    30% {
      -webkit-transform: translateX(4%) rotate(4deg);
              transform: translateX(4%) rotate(4deg);
    }
    100% {
      -webkit-transform: translateX(-200%) rotate(-90deg);
              transform: translateX(-200%) rotate(-90deg);
      opacity: 0;
    }
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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 inactive">
    <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>
like image 194
Angelo Lucas Avatar answered Sep 19 '22 18:09

Angelo Lucas


Here is one way that it can be achieved.

$('.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-modal .overlay .modal {
width: 450px;
box-shadow: 0px 23px 30px -20px rgba(0, 0, 0, 0.4);
}
.section-modal .overlay .modal .title {
background: #0097A7;
color: white;
border-radius: 4px 4px 0px 0px;
text-align: center;
line-height: 48px;
font-weight: 700;
}
.section-modal .overlay .modal .body {
background: white;
border-radius: 0px 0px 4px 4px;
line-height: 20px;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-align: stretch;
-webkit-align-items: stretch;
-ms-flex-align: stretch;
align-items: stretch;
}
.section-modal .overlay .modal .body .text {
padding: 30px;
}
.section-modal .overlay .modal .body .text p {
margin-bottom: 20px;
}
.section-modal .overlay .modal .body .img {
height: 180px;
width: 140px;
border-bottom-left-radius: 4px;
-webkit-flex-shrink: 0;
-ms-flex-negative: 0;
flex-shrink: 0;
background-size: cover;
background-position: center;
}
.section-modal .overlay.is-active {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-animation: overlayAnim 5s ease-in-out alternate;
animation: overlayAnim 5s ease-in-out alternate;
}
.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 .modal {
-webkit-animation: modalAnim 5s ease-in-out alternate;
animation: modalAnim 5s ease-in-out alternate;
}
.section-modal .overlay.is-active.hidden .modal {
-webkit-animation: modalAnim 2s ease-in-out reverse;
animation: modalAnim 2s ease-in-out reverse;
}

@keyframes overlayAnim {
0%, 100% {
	background-color: transparent;
}
15%, 85% {
	background-color: rgba(0, 0, 0, 0.3);
}
}
@keyframes modalAnim {
0% {
	-webkit-transform: translateX(-200%) rotate(-90deg);
	transform: translateX(-200%) rotate(-90deg);
	opacity: 0;
}
10% {
	-webkit-transform: translateX(4%) rotate(4deg);
	transform: translateX(4%) rotate(4deg);
}
15%, 100% {
	-webkit-transform: translateX(0%);
	transform: translateX(0%);
	opacity: 1;
}

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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 main difference is here, where I apply a reverse animation to the current active class, when there is a hidden class added to it:

.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;
}

And here, where I check if there is an active class, adding a hidden class instantly if it's true and waiting for the animation to complete before removing both of them:

if ($overlay.hasClass('is-active')) {
    $overlay.addClass('hidden').on('animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd', function(){
        $overlay.removeClass('is-active hidden');
    })
} else {
    $overlay.addClass('is-active');
}

I think that this will do the trick.

like image 24
drip Avatar answered Sep 19 '22 18:09

drip