CSS动画:轻轻过渡删除类

Zkk*_*Zkk 7 css jquery animation css3

在此代码中,模式通过轻微转换(CSS3)调用.当我点击"Clode Modal"课程时.is-active被删除.但是,它会立即被删除.我想做相反的效果.

打开模态时,它从左到右显示.当我关闭模态时,我希望他做相反的效果.他在哪里(中)到左边.

我怎样才能做到这一点?

$('.section-modal .profile, .section-modal .overlay .modal .title').click(function(){
  $('.overlay').toggleClass('is-active');
});
Run Code Online (Sandbox Code Playgroud)
.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;
  }

}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

dri*_*rip 1

这是实现这一目标的一种方法。

$('.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');
}
});
Run Code Online (Sandbox Code Playgroud)
.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;
}

}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

主要区别在于,当添加了隐藏类时,我将反向动画应用于当前活动类:

.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;
}
Run Code Online (Sandbox Code Playgroud)

在这里,我检查是否存在活动类,如果存在,则立即添加一个隐藏类,并等待动画完成,然后再删除它们:

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

我认为这会成功。