Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The effect you're looking for is a little tricky to achieve, but it can be done, even without a wrapper element.</p> <p>The main issue here is that elements naturally render top-down, not bottom-up. Animating both the <code>top</code> and <code>height</code> CSS properties of a relatively-positioned element allows us to implement a slide-up effect, but the element will still render top-down:</p> <pre><code>+-------------------------------------------+ | | ^ | | | Hidden area collapses upwards. | | | +-------------------------------------------+ &lt;-- 'top' | | ^ | | Upper part of element (visible). | | | | | | Animation goes bottom-up. | | Element still renders top-down. | | | | | | +--|----------------------------------------+ &lt;-- 'top + height' | | | | | | Lower part of element (hidden). | | | V | | +-------------------------------------------+ </code></pre> <p>If we want to simulate bottom-up rendering, we have to modify the <a href="https://developer.mozilla.org/en/DOM/element.scrollTop" rel="noreferrer">scrollTop</a> property of the element during the animation, in order for its lower part to always remain in view:</p> <pre><code>+-------------------------------------------+ | | ^ | | Upper part of element (hidden). | | Hidden area collapses upwards. | | | | +--|----------------------------------------+ &lt;-- 'top' and 'scrollTop' | | | ^ | | Element still renders top-down. | | | | | | Animation goes bottom-up. | | Lower part of element (visible). | | | V | | +-------------------------------------------+ &lt;-- 'top + height' </code></pre> <p>We can use <a href="http://api.jquery.com/animate/" rel="noreferrer">animate()</a> with <code>scrollTop</code>, but doing so in conjunction with <code>top</code> and <code>height</code> did not work correctly in my tests (I suspect <code>scrollTop</code> is reset when <code>top</code> or <code>height</code> are modified in the first animation step, so it ends up stuck to <code>0</code>).</p> <p>To work around this, we can handle <code>scrollTop</code> ourselves through the optional <a href="http://api.jquery.com/animate/#step" rel="noreferrer">step</a> function we can pass to <code>animate()</code>. This function is called with two arguments, <code>now</code> and <code>fx</code>, <code>now</code> being the current value of the animated property and <code>fx</code> being a wrapper object around useful information, like the element and property being animated.</p> <p>Since we always want <code>scrollTop</code> to be the same as <code>top</code>, we only have to test if <code>top</code> is being animated in our <code>step</code> function. If it is, we set <code>scrollTop</code> to <code>now</code>. This solution gives acceptable results, although it flickers a little too much for my taste (that might be an artifact of my browser, though).</p> <p>So, in summary, to implement that effect, we have to:</p> <ul> <li>Fetch the element's original height,</li> <li>Make the element <code>position: relative;</code> so we can animate its <code>top</code> property,</li> <li>Collapse the element by setting <code>top</code> to the original height and <code>height</code> to <code>0</code>,</li> <li>Show the element (necessary in your fiddle since <code>display: none;</code> is applied),</li> <li>Animate <code>top</code> to <code>0</code> and <code>height</code> to the original height,</li> <li>Give <code>scrollTop</code> the value of <code>top</code> on each animation step.</li> </ul> <p>Resulting in the following code:</p> <pre><code>$("#click").click(function() { var $revealMe = $("#revealMe"); var originalHeight = $revealMe.height(); $revealMe.css({ position: "relative", top: originalHeight, height: 0 }).show().animate({ top: 0, height: originalHeight }, { duration: 1000, step: function(now, fx) { if (fx.prop == "top") { $(fx.elem).scrollTop(now); } } }); }); </code></pre> <p>You can test it in <a href="http://jsfiddle.net/gBY8V/1/" rel="noreferrer">this fiddle</a>.</p>
    singulars
    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. 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