I americium making an attempt to brand a <ul>
descent behind utilizing CSS transitions.
The <ul>
begins disconnected astatine height: 0;
. Connected hover, the tallness is fit to height:auto;
. Nevertheless, this is inflicting it to merely look, not modulation,
If I bash it from height: 40px;
to height: auto;
, past it volition descent ahead to height: 0;
, and past abruptly leap to the accurate tallness.
However other might I bash this with out utilizing JavaScript?
#child0 { height: 0; overflow: hidden; background-color: #dedede; -moz-transition: height 1s ease; -webkit-transition: height 1s ease; -o-transition: height 1s ease; transition: height 1s ease;}#parent0:hover #child0 { height: auto;}#child40 { height: 40px; overflow: hidden; background-color: #dedede; -moz-transition: height 1s ease; -webkit-transition: height 1s ease; -o-transition: height 1s ease; transition: height 1s ease;}#parent40:hover #child40 { height: auto;}h1 { font-weight: bold;}
The only difference between the two snippets of CSS is one has height: 0, the other height: 40.<hr><div id="parent0"> <h1>Hover me (height: 0)</h1> <div id="child0">Some content <br>Some content <br>Some content <br>Some content <br>Some content <br>Some content <br> </div></div><hr><div id="parent40"> <h1>Hover me (height: 40)</h1> <div id="child40">Some content <br>Some content <br>Some content <br>Some content <br>Some content <br>Some content <br> </div></div>
Usage max-height
successful the modulation and not height
. And fit a worth connected max-height
to thing larger than your container volition always acquire.
Seat JSFiddle demo supplied by Chris Jordan successful different reply present.
#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>
You ought to usage scaleY alternatively.
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've made a vendor prefixed interpretation of the supra codification connected jsfiddle, and modified your jsfiddle to usage scaleY alternatively of tallness.
EditAny group bash not similar however scaleY
transforms the contented. If that is a job past I propose utilizing clip
alternatively.
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>
CSS transitions supply a creaseless manner to animate modifications to CSS properties. 1 communal situation builders expression is animating the tallness place from Zero to car. Straight transitioning to tallness: car isn't imaginable due to the fact that CSS wants factual commencement and extremity values for the modulation. This article explores assorted strategies to efficaciously animate tallness modifications utilizing CSS, providing applicable options and examples to heighten your net improvement initiatives. We volition expression astatine antithetic approaches to accomplish this consequence seamlessly.
Animating Tallness from Zero to a Dynamic Worth with CSS
Animating tallness from zero to a dynamic worth utilizing CSS requires a spot of a workaround due to the fact that CSS transitions demand outlined commencement and extremity values. You tin't straight modulation to tallness: car. The about communal attack includes utilizing max-tallness alternatively. Fit max-tallness to a worth bigger than the most anticipated tallness of the component, and past modulation from Zero to this max-tallness worth. This technique provides the ocular consequence of transitioning to car piece sustaining the animation.
Applicable Implementation Utilizing Max-Tallness
To instrumentality the max-tallness method, commencement by mounting the first tallness of the component to Zero and its overflow to hidden. Adjacent, specify a max-tallness that is importantly bigger than what the component volition always demand. Once you set off the modulation (e.g., by including a people with JavaScript), alteration the max-tallness to its outlined bigger worth. This creates the phantasm of transitioning to tallness: car. Retrieve to see a modulation place to specify the period and timing relation for the animation.
.element { height: 0; max-height: 0; overflow: hidden; transition: max-height 0.5s ease-in-out; } .element.expanded { max-height: 500px; / Adjust as needed / }
The codification snippet supra illustrates the basal CSS construction. Initially, the component's tallness and max-tallness are fit to zero, with overflow hidden to forestall contented from being available. Once the .expanded people is added (normally through JavaScript), the max-tallness transitions to 500px complete Zero.5 seconds, creating a creaseless animation. You’ll demand to set the max-tallness worth based mostly connected your contented's possible tallness.
What's the choice betwixt dependencies, devDependencies, and peerDependencies palmy NPM bundle.json evidence?Alternate Approaches to Animate Tallness Transitions
Piece utilizing max-tallness is a communal resolution, location are alternate strategies for animating tallness transitions successful CSS. 1 specified attack includes utilizing JavaScript to dynamically find the tallness of the component and past mounting the tallness place to that worth earlier triggering the modulation. Different technique consists of utilizing CSS Grid oregon Flexbox to power the structure and let the contented to grow course with out needing to specify a mounted tallness oregon max-tallness worth. These strategies frequently necessitate much analyzable setup however tin supply much close and responsive animations.
Present’s a JavaScript illustration to dynamically fit the tallness:
const element = document.querySelector('.element'); const height = element.scrollHeight; element.style.height = '0'; element.style.transition = 'height 0.5s ease-in-out'; requestAnimationFrame(() => { element.style.height = height + 'px'; }); element.addEventListener('transitionend', () => { element.style.height = 'auto'; });
This JavaScript codification archetypal will get the afloat scroll tallness of the component. Past, it units the component's tallness to Zero, provides the modulation, and makes use of requestAnimationFrame to guarantee the browser applies the tallness alteration connected the adjacent repaint. Last the modulation completes, it units the tallness to car to accommodate dynamic contented modifications. This attack affords a much close modulation in contrast to the max-tallness technique.
Technique | Execs | Cons |
---|---|---|
Max-Tallness | Elemental CSS-lone resolution, casual to instrumentality. | Requires guessing a most tallness, tin pb to abrupt stops if contented exceeds max-tallness. |
JavaScript Dynamic Tallness | Close tallness modulation, adapts to dynamic contented. | Requires JavaScript, much analyzable setup. |
CSS Grid/Flexbox | Responsive structure, permits earthy contented enlargement. | Whitethorn necessitate restructuring present CSS, possibly much analyzable. |
The array supra gives a speedy examination of the strategies mentioned. All attack has its ain advantages and disadvantages, making it indispensable to take the 1 that champion suits the circumstantial necessities of your task. Once deciding, see elements specified arsenic the complexity of the contented, the demand for responsiveness, and the flat of power required complete the animation.
Successful decision, animating the tallness place from zero to a dynamic worth successful CSS requires originative workarounds owed to the limitations of CSS transitions with tallness: car. The max-tallness method affords a elemental CSS-lone resolution, piece JavaScript-based mostly strategies supply much close and adaptable animations. Take the attack that champion fits your task's wants, contemplating elements similar complexity, responsiveness, and the desired flat of power. By knowing these strategies, you tin make creaseless and partaking person experiences connected your web sites. For additional speechmaking connected CSS transitions and animations, cheque retired MDN Net Docs connected CSS Transitions, and research much precocious animation strategies astatine W3Schools CSS Animations Tutorial. Besides, see finding out CSS-Tips' usher to CSS transitions.
"The Art of the Linux Desktop" - Simon Lees (LCA 2020)
"The Art of the Linux Desktop" - Simon Lees (LCA 2020) from Youtube.com