Sadly, but an animation between block
and none
is impossible.
Instead, you can use two steps animation with opacity
.
Here's an example with a css bias.
HTML:
...
<div class="block opacity hidden">...</div>
...
CSS (don't forget to add transition
):
.block {
display: block;
opacity: 1;
}
.block.opacity {
opacity: 0;
}
.block.hidden {
display: none;
}
jQuery:
$('.block').removeClass('hidden'); // the block is still invisible
setTimeout( function() {
$('.block').removeClass('opacity'); // now the block is visible
}, 100); // small delay is needed, better if it's equal the transition time
It's simple. As the alternatives you can use .fadeIn()
and .fadeOut()
methods or .animate()
.
UPD
You should remember that timeout in the reverse function have to equals the duration of transition
, or the element will be hidden before the end of the animation.
$('.block').addClass('opacity');
setTimeout( function() {
$('.block').addClass('hidden');
}, 100);
UPD2
JS:
var el = document.getElementsByClassName('block');
el.classList.remove('hidden');
setTimeout( function() {
el.classList.remove('opacity');
}, 100);