I'm making a modal using CSS3 and alternating classes with jQuery.
我正在使用CSS3制作模态,并使用jQuery交替使用类。
The modal open correctly (with css3 effect) but my trouble is when I close the modal. I cannot make the modal close with the css3 effect in reverse way. In other words, I want open the modal with the effect and close the modal with it in reverse way.
模态正确打开(具有css3效果),但我的麻烦是当我关闭模态时。我不能以相反的方式使模态与css3效果接近。换句话说,我想用效果打开模态并以相反的方式关闭模态。
The modal open 0%: -70deg%´ ---- ´100%: 0
. I try create an animation
called closemodal
with reverse effect. 0%: 0
---- 100%: -70deg
but without successful.
模态打开0%: - 70deg%'----'100%:0。我尝试使用反向效果创建一个名为closemodal的动画。 0%:0 ---- 100%: - 70deg但没有成功。
How I can open and close the modal with effect appearing... and effect coming out?
我如何打开和关闭模式效果出现...效果出来?
Pls, looks the snippet:
请看看片段:
$(document).ready(function(){
$('.modal-open').click(function(){
$('.overlay').addClass('modal-show');
$('.modal').addClass('modal-show modal-perspective');
$('.modal-content').addClass('modal-animate');
});
$('.modal-close').click(function(){
$('.overlay').removeClass('modal-show');
$('.modal').removeClass('modal-show modal-perspective');
$('.modal-content').removeClass('modal-animate');
$('.modal-content').addClass('modal-remove');
});
});
html, body{
margin: 0;
padding: 0;
box-sizing: border-box;
background: #FF3300;
font-family: 'Ubuntu', sans-serif;
}
.modal{
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50%;
height: auto;
min-width: 300px;
z-index: 2;
visibility: hidden;
display: flex;
align-items: center;
justify-content: center;
}
.modal-content{
padding: 120px 10px;
text-align: center;
color: #000;
background: #bd330f;
border-radius: 10px;
border: 1px solid #909090;
box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.9);
}
.modal-open{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: transparent;
background: #fff;
font-size: 15px;
font-weight: bold;
padding: 20px 50px;
border-radius: 5px;
}
.modal-open:active{
outline: transparent;
}
.modal-close{
padding: 8px 40px;
border-radius: 8px;
border: transparent;
background: #212121;
color: #fff;
outline: transparent;
}
.overlay {
position: fixed;
width: 100%;
height: 100%;
visibility: hidden;
top: 0;
left: 0;
z-index: 1;
opacity: 0;
background: rgba(0, 0, 0, 0.5);
transition: all 0.7s;
}
.modal-show {
opacity: 1;
visibility: visible;
}
.modal-perspective{
-webkit-perspective: 900px;
perspective: 900px;
}
.modal-animate{
animation: openmodal 300ms ease-in forwards;
}
.modal-remove{
animation: closemodal 300ms ease-in-out forwards;
}
@keyframes openmodal{
0%{
transform: rotateX(-70deg);
-webkit-transform: rotateX(-70deg);
-moz-transform: rotateX(-70deg);
-ms-transform: rotateX(-70deg);
}
100%{
transform: rotateX(0deg);
-webkit-transform: rotateX(0deg);
-moz-transform: rotateX(0deg);
-ms-transform: rotateX(0deg);
}
}
@keyframes closemodal{
0%{
transform: rotateX(0deg);
-webkit-transform: rotateX(0deg);
-moz-transform: rotateX(0deg);
-ms-transform: rotateX(0deg);
}
100%{
transform: rotateX(-70deg);
-webkit-transform: rotateX(-70deg);
-moz-transform: rotateX(-70deg);
-ms-transform: rotateX(-70deg);
}
}
Modal Dialog
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi gravida libero nec velit. Morbi scelerisque luctus velit. Etiam dui sem, fermentum vitae, sagittis id, malesuada in, quam. Proin mattis lacinia justo. Vestibulum facilisis auctor urna. Aliquam in lorem sit amet leo accumsan.
In addition, in my jQuery code, there are a better way to toggle the classes? I think that is a better way to toggle them. How I can make it?
另外,在我的jQuery代码中,有一种更好的方法可以切换类吗?我认为这是一种更好的方式来切换它们。我怎么能做到的?
2
You must avoid using CSS3 keyframe animations when you need the reverse effect. What you really should use (and need) is CSS3 transition which is capable of producing the reverse effect whenever the class that adds the forward effect is removed.
当您需要反向效果时,必须避免使用CSS3关键帧动画。你真正应该使用(和需要)的是CSS3过渡,它能够在删除添加前向效果的类时产生相反的效果。
Just set the transform: rotateX(-70deg)
to the original state (that is, .modal-content
) and then set the transform: rotateX(0deg)
on the open state (.modal-animate
). This means the element will be invisible originally and gets displayed when open. Finally add the transition
property to the element.
只需将transform:rotateX(-70deg)设置为原始状态(即.modal-content),然后在打开状态(.modal-animate)上设置transform:rotateX(0deg)。这意味着元素最初是不可见的,并在打开时显示。最后将transition属性添加到元素中。
(Note: As discussed in comments and in this post it is generally not good to use all
in transition
but the impact of that in this example is very minimal in my opinion.)
(注意:正如评论和本文中所讨论的那样,在转换过程中使用all并不是一件好事,但在我看来,这个示例中的影响非常小。)
$(document).ready(function() {
$('.modal-open').click(function() {
$('.overlay').addClass('modal-show');
$('.modal').addClass('modal-show modal-perspective');
$('.modal-content').addClass('modal-animate');
});
$('.modal-close').click(function() {
$('.overlay').removeClass('modal-show');
$('.modal').removeClass('modal-show modal-perspective');
$('.modal-content').removeClass('modal-animate');
});
});
html,
body {
margin: 0;
padding: 0;
box-sizing: border-box;
background: #FF3300;
font-family: 'Ubuntu', sans-serif;
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50%;
height: auto;
min-width: 300px;
z-index: 2;
visibility: hidden;
display: flex;
align-items: center;
justify-content: center;
}
.modal-content {
padding: 120px 10px;
text-align: center;
color: #000;
background: #bd330f;
border-radius: 10px;
border: 1px solid #909090;
box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.9);
transition: all 300ms ease-in;
/* added for original state - vendor prefixed version should come before unprefixed property */
-webkit-transform: rotateX(-70deg);
-moz-transform: rotateX(-70deg);
-ms-transform: rotateX(-70deg);
transform: rotateX(-70deg);
}
.modal-open {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: transparent;
background: #fff;
font-size: 15px;
font-weight: bold;
padding: 20px 50px;
border-radius: 5px;
}
.modal-open:active {
outline: transparent;
}
.modal-close {
padding: 8px 40px;
border-radius: 8px;
border: transparent;
background: #212121;
color: #fff;
outline: transparent;
}
.overlay {
position: fixed;
width: 100%;
height: 100%;
visibility: hidden;
top: 0;
left: 0;
z-index: 1;
opacity: 0;
background: rgba(0, 0, 0, 0.5);
transition: all 0.7s;
}
.modal-show {
opacity: 1;
visibility: visible;
}
.modal-perspective {
-webkit-perspective: 900px;
perspective: 900px;
}
.modal-animate {
transition: all 300ms ease-in-out;
/* remove animation, set below for open state */
-webkit-transform: rotateX(0deg);
-moz-transform: rotateX(0deg);
-ms-transform: rotateX(0deg);
transform: rotateX(0deg);
}
Modal Dialog
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi gravida libero nec velit. Morbi scelerisque luctus velit. Etiam dui sem, fermentum vitae, sagittis id, malesuada in, quam. Proin mattis lacinia justo. Vestibulum facilisis auctor urna.
Aliquam in lorem sit amet leo accumsan.