CSS fadein fadeout onclick

I am trying to use a css animation on a div that is shown / hidden using toggle ().

I added some ease to my animation, but it's only fadesIn and not fadeOut.

Here is my css

#form {
display: none;
animation: formFade 2s ease-in-out;
-moz-animation: formFade 2s ease-in-out; /* Firefox */
-webkit-animation: formFade 2s ease-in-out; /* Safari and Chrome */
-o-animation: formFade 2s ease-in-out; /* Opera */
}

@keyframes formFade {
from {
    opacity:0;
}
to {
    opacity:1;
}
}
@-moz-keyframes formFade { /* Firefox */
from {
    opacity:0;
}
to {
    opacity:1;
}
}
@-webkit-keyframes formFade { /* Safari and Chrome */
from {
    opacity:0;
}
to {
    opacity:1;
}
}
@-o-keyframes formFade { /* Opera */
from {
    opacity:0;
}
to {
    opacity: 1;
}
}

      

and here is the html / js

<form id="form" >
TEST
</form>

<script>
            $('#formButton').on('click', function() {
            $("#form").toggle();

            });
        </script>  

      

It disappears in onclick but doesn't disappear. Any ideas why?

+3


source to share


1 answer


Using .toggle()

to hide an element simply sets it to display: none

, without affecting the opacity. Try the following:



$('#formButton').on('click', function() {
    if ($('#form').css('opacity') == 0) $('#form').css('opacity', 1);
    else $('#form').css('opacity', 0);
});
      

#form {
    opacity: 0;
    -webkit-transition: all 2s ease-in-out;
    -moz-transition: all 2s ease-in-out;
    -ms-transition: all 2s ease-in-out;
    -o-transition: all 2s ease-in-out;
    transition: all 2s ease-in-out;
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="form">Test</div>

<a href="#" id="formButton">Click</a>
      

Run codeHide result


+5


source







All Articles