扭转css动画

时间:2015-10-22 13:42:43

标签: css css-animations reverse

我有一个叠加层,它使用以下代码淡入

.overlay {
    background: rgba(32, 79, 156, 0.97);
    width: 100%;
    height: 100vh;
    position: fixed;
    z-index: 1000;
   display: none;


}

.overlay.active {
    display: block;
     -webkit-animation-duration: 0.5s;
    -webkit-animation-name: fadeIn;
     animation-duration: 0.5s;
    animation-name: fadeIn;
}





@keyframes fadeIn {
    0% {
        display:none; 
        opacity: 0;
    }

    1% {
        display: block ; 
        opacity: 0;
    }

    100% {
        display: block ; 
        opacity: 1;
    }
}

我所有的js都是切换课程

$('.hamburger').on('click', function(){
       $('.overlay, .hamburger').toggleClass('active');
    });

如何撤消此动画?此刻它只是跳回到以前的状态。

2 个答案:

答案 0 :(得分:1)

动画无法在display属性上工作,因为display:none隐藏了元素,因此即使动画正在运行,您也不会看到它褪色。您可以使用CSS Transitions简单地为opacity属性设置动画,如下所示:



$('.hamburger').on('click', function(){
       $('.overlay, .hamburger').toggleClass('active');
    });

.overlay {
    background: rgba(32, 79, 156, 0.97);
    width: 100%;
    height: 100vh;
    position: fixed;
    z-index: 1000;
    opacity:0;
    pointer-events:none;
    -webkit-transition: scale, opacity 0.5s ease;
    -moz-transition: scale, opacity 0.5s ease;
    -o-transition: scale, opacity 0.5s ease;
    transition: scale, opacity 0.5s ease;
}

.overlay.active {
    opacity:1;
    pointer-events:auto;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hamburger">hamburger</div>
<div class="overlay">overlay</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您的toogle按钮会隐藏,因为叠加层与按钮重叠,请尝试将覆盖层上方的z-index设置为更高的按钮。通过动画,您可以使用opacitytransition

<强> CSS

.hamburger{
    position: relative;
    z-index: 1001;
}

.hamburger.active{
    color: #fff;
    transition: all 0.6s ease;
}

.overlay {
    background: rgba(32, 79, 156, 0.97);
    width: 100%;
    height: 100vh;
    position: fixed;
    z-index: 1000;
    opacity:0;
    transition: all 0.5s ease;    
}

.overlay.active {
    display: block;
    transition: all 0.5s ease;
    opacity:1;
}

<强> DEMO HERE