CSS Animation and Display None
There are a few answers already, but here is my solution:
I use opacity: 0
and visibility: hidden
. To make sure that visibility
is set before the animation, we have to set the right delays.
I use http://lesshat.com to simplify the demo, for use without this just add the browser prefixes.
(e.g. -webkit-transition-duration: 0, 200ms;
)
.fadeInOut {
.transition-duration(0, 200ms);
.transition-property(visibility, opacity);
.transition-delay(0);
&.hidden {
visibility: hidden;
.opacity(0);
.transition-duration(200ms, 0);
.transition-property(opacity, visibility);
.transition-delay(0, 200ms);
}
}
So as soon as you add the class hidden
to your element, it will fade out.
CSS (or jQuery, for that matter) can't animate between display: none;
and display: block;
. Worse yet: it can't animate between height: 0
and height: auto
. So you need to hard code the height (if you can't hard code the values then you need to use javascript, but this is an entirely different question);
#main-image{
height: 0;
overflow: hidden;
background: red;
-prefix-animation: slide 1s ease 3.5s forwards;
}
@-prefix-keyframes slide {
from {height: 0;}
to {height: 300px;}
}
You mention that you're using Animate.css, which I'm not familiar with, so this is a vanilla CSS.
You can see a demo here: http://jsfiddle.net/duopixel/qD5XX/
I had the same problem, because as soon as display: x;
is in animation, it won't animate.
I ended up in creating custom keyframes, first changing the display
value then the other values. May give a better solution.
Or, instead of using display: none;
use position: absolute; visibility: hidden;
It should work.
You can manage to have a pure CSS implementation with max-height
#main-image{
max-height: 0;
overflow: hidden;
background: red;
-prefix-animation: slide 1s ease 3.5s forwards;
}
@keyframes slide {
from {max-height: 0;}
to {max-height: 500px;}
}
You might have to also set padding
, margin
and border
to 0, or simply padding-top
, padding-bottom
, margin-top
and margin-bottom
.
I updated the demo of Duopixel here : http://jsfiddle.net/qD5XX/231/