I have got a problem with a CSS3 animation.
.child {
opacity: 0;
display: none;
-webkit-transition: opacity 0.5s ease-in-out;
-moz-transition: opacity 0.5s ease-in-out;
transition: opacity 0.5s ease-in-out;
}
.parent:hover .child {
opacity: 0.9;
display: block;
}
This code only works if I remove the change of display
.
I want to change the display just after the hover but the opacity should be changed using the transition.
I changed a bit but the result is beautiful.
Thank you to everyone.
Based on Michaels answer this is the actual CSS code to use
I had the same problem. I tried using animations instead of transitions - as suggested by @MichaelMullany and @Chris - but it only worked for webkit browsers even if I copy-pasted with "-moz" and "-o" prefixes.
I was able to get around the problem by using
visibility
instead ofdisplay
. This works for me because my child element isposition: absolute
, so document flow isn't being affected. It might work for others too.This is what the original code would look like using my solution:
If possible - use
visibility
instead ofdisplay
For instance:
One thing that I did was set the initial state's margin to be something like "margin-left: -9999px" so it does not appear on the screen, and then reset "margin-left: 0" on the hover state. Keep it "display: block" in that case. Did the trick for me :)
Edit: Save the state and not revert to previous hover state? Ok here we need JS:
If you are triggering the change with JS, let's say on click, there is a nice workaround.
You see the problem happens because the animation is ignored on display:none element but browser applies all the changes at once and the element is never display:block while not animated at the same time.
The trick is to ask the browser to render the frame after changing the visibility but before triggering the animation.
Here is a JQuery example: