在此代码中,使用轻微转换(CSS3)调用模态。当我点击"Clode Modal"
时,课程.is-active
将被删除。但是,它会立即被删除。我想做相反的效果。
打开模态时,它从左到右显示。当我关闭模态时,我希望他做相反的效果。他在哪里(中间)到左边。
我该怎么做?
$('.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>
答案 0 :(得分:4)
您需要与输出效果所需时间相关的延迟。只有css transition-delay 才能执行此操作。
我为输入效果( modalAnimIn )和输出效果( modalAnimOut )创建了一个CSS动画。我不得不在js中做一个小的改动来调用输出中的modalAnimOut,改变类而不是删除。
注意:没有必要为overley创建动画。使用更简单的转换
$('.section-modal .profile, .section-modal .overlay .modal .title').click(function(){
$('.overlay').toggleClass('inactive is-active');
});
&#13;
.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;
}
}
&#13;
<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>
&#13;
答案 1 :(得分:1)
使用“linear”(或“swing”)字符串值将可选的“easing”字符串输入传递给toggleClass方法。
$('.overlay').toggleClass('is-active',,"linear");
有关详细信息,请参阅jQuery toggleClass page。
答案 2 :(得分: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');
}
});
&#13;
.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;
}
}
&#13;
<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>
&#13;
主要区别在于,当我向当前活动类应用反向动画时,会添加一个隐藏类:
.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;
}
在这里,我检查是否存在活动类,如果它是真的则立即添加一个隐藏类,并在移除它们之前等待动画完成:
if ($overlay.hasClass('is-active')) {
$overlay.addClass('hidden').on('animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd', function(){
$overlay.removeClass('is-active hidden');
})
} else {
$overlay.addClass('is-active');
}
我认为这样就可以了。