$('#button1').click(function() {
$('.square').removeClass('fadeMeIn').addClass('fadeMeOut');
});
$('#button2').click(function() {
$('.square').removeClass('fadeMeOut').addClass('fadeMeIn');
});
.square {
height: 50px;
width: 50px;
margin: 10px;
background-color: rgba(255, 0, 0, 0.8);
}
.fadeMeOut {
-webkit-animation: faeOutRGBA 1s linear;
-moz-animation: faeOutRGBA 1s linear;
-o-animation: faeOutRGBA 1s linear;
animation: faeOutRGBA 1s linear;
background-color: rgba(255, 0, 0, 0);
}
.fadeMeIn {
-webkit-animation: faeInRGBA 1s linear;
-moz-animation: faeInRGBA 1s linear;
-o-animation: faeInRGBA 1s linear;
animation: faeInRGBA 1s linear;
background-color: rgba(255, 0, 0, 0.8);
}
@-webkit-keyframes faeOutRGBA {
0% {
background-color: rgba(255, 0, 0, 0.8)
}
100% {
background-color: rgba(255, 0, 0, 0);
}
}
@-moz-keyframes faeOutRGBA {
0% {
background-color: rgba(255, 0, 0, 0.8);
}
100% {
background-color: rgba(255, 0, 0, 0);
}
}
@-o-keyframes faeOutRGBA {
0% {
background-color: rgba(255, 0, 0, 0.8);
}
100% {
background-color: rgba(255, 0, 0, 0);
}
}
@keyframes faeOutRGBA {
0% {
background-color: rgba(255, 0, 0, 0.8);
}
100% {
background-color: rgba(255, 0, 0, 0);
}
;
}
@-webkit-keyframes faeInRGBA {
0% {
background-color: rgba(255, 0, 0, 0)
}
100% {
background-color: rgba(255, 0, 0, 0.8);
}
}
@-moz-keyframes faeInRGBA {
0% {
background-color: rgba(255, 0, 0, 0);
}
100% {
background-color: rgba(255, 0, 0, 0.8);
}
}
@-o-keyframes faeInRGBA {
0% {
background-color: rgba(255, 0, 0, 0);
}
100% {
background-color: rgba(255, 0, 0, 0.8);
}
}
@keyframes faeInRGBA {
0% {
background-color: rgba(255, 0, 0, 0);
}
100% {
background-color: rgba(255, 0, 0, 0.8);
}
;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="square"></div>
<button id="button1">fade out</button>
<button id="button2">fade in</button>