Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can, with a little bit of non-semantic jiggery-pokery. My usual approach is to animate the height of an outer DIV which has a single child which is a style-less DIV used only for measuring the content height.</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>function growDiv() { var growDiv = document.getElementById('grow'); if (growDiv.clientHeight) { growDiv.style.height = 0; } else { var wrapper = document.querySelector('.measuringWrapper'); growDiv.style.height = wrapper.clientHeight + "px"; } }</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>#grow { -moz-transition: height .5s; -ms-transition: height .5s; -o-transition: height .5s; -webkit-transition: height .5s; transition: height .5s; height: 0; overflow: hidden; outline: 1px solid red; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input type="button" onclick="growDiv()" value="grow"&gt; &lt;div id='grow'&gt; &lt;div class='measuringWrapper'&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</code></pre> </div> </div> </p> <p>One would like to just be able to dispense with the <code>.measuringWrapper</code> and just set the DIV's height to auto and have that animate, but that doesn't seem to work (the height gets set, but no animation occurs).</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>function growDiv() { var growDiv = document.getElementById('grow'); if (growDiv.clientHeight) { growDiv.style.height = 0; } else { growDiv.style.height = 'auto'; } }</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>#grow { -moz-transition: height .5s; -ms-transition: height .5s; -o-transition: height .5s; -webkit-transition: height .5s; transition: height .5s; height: 0; overflow: hidden; outline: 1px solid red; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input type="button" onclick="growDiv()" value="grow"&gt; &lt;div id='grow'&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;div&gt; The contents of my div. &lt;/div&gt; &lt;/div&gt;</code></pre> </div> </div> </p> <p>My interpretation is that an explicit height is needed for the animation to run. You can't get an animation on height when either height (the start or end height) is <code>auto</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload