P粉3110892792023-08-22 09:21:07
You need to hide the element in other ways to make it work properly.
I achieved the effect by setting both <div>
to absolute positioning, and the hidden one to opacity: 0
.
If you switch the display
attribute from none
to block
, the transition effect for other elements will not occur.
To solve this problem, always allow elements to be display: block
, but hide the element by any of the following means:
height
to 0
. opacity
to 0
. overflow: hidden
. There may be more solutions, but if you switch the element to display: none
, the transition cannot be performed. For example, you might try something similar to:
div { display: none; transition: opacity 1s ease-out; opacity: 0; } div.active { opacity: 1; display: block; }
But this will notwork. In my experience, I've found that this doesn't have any effect.
So you always need to keep the element's display: block
- but you can get around this by:
div { transition: opacity 1s ease-out; opacity: 0; height: 0; overflow: hidden; } div.active { opacity: 1; height: auto; }
P粉7291982072023-08-22 00:45:19
You can connect two or more transition effects, and visibility
comes in handy at this point.
div { border: 1px solid #eee; } div > ul { visibility: hidden; opacity: 0; transition: visibility 0s, opacity 0.5s linear; } div:hover > ul { visibility: visible; opacity: 1; }
<div> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> </div>