P粉0557261462023-08-28 10:37:41
You should use scaleY.
ul { background-color: #eee; transform: scaleY(0); transform-origin: top; transition: transform 0.26s ease; } p:hover ~ ul { transform: scaleY(1); }
<p>Hover This</p> <ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ul>
I made a vendor-prefixed version of the above code on jsfiddle and changed your jsfiddle to use scaleY instead of height.
edit
Some people don't like the way scaleY
converts content. If this is a problem then I'd recommend using clip
instead.
ul { clip: rect(auto, auto, 0, auto); position: absolute; margin: -1rem 0; padding: .5rem; color: white; background-color: rgba(0, 0, 0, 0.8); transition-property: clip; transition-duration: 0.5s; transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275); } h3:hover ~ ul, h3:active ~ ul, ul:hover { clip: rect(auto, auto, 10rem, auto); }
<h3>Hover here</h3> <ul> <li>This list</li> <li>is clipped.</li> <li>A clip transition</li> <li>will show it</li> </ul> <p> Some text... </p>
P粉2427419212023-08-28 00:29:46
Use max-height
in transitions instead of height
. And set the value of max-height
to something larger than your box.
See Chris Jordan's answer to here in another JSFiddle demo /stackoverflow.com/a/20226830/18706">
#menu #list { max-height: 0; transition: max-height 0.15s ease-out; overflow: hidden; background: #d5d5d5; } #menu:hover #list { max-height: 500px; transition: max-height 0.25s ease-in; }
<div id="menu"> <a>hover me</a> <ul id="list"> <!-- Create a bunch, or not a bunch, of li's to see the timing. --> <li>item</li> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ul> </div>