Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd also like to chime in on this old thread, if I may, in case my solution helps anyone. My specific situation is this: I have some div's that are set with a max-height value that limits them to three lines tall, and when the user mouseovers them I want them to expand to their natural height; and when the mouse cursor leaves the div, I want them to shrink back down to the clipped, max-three-lines-tall height. I need to use the CSS max-height property, rather than height, because I have some div's that contain only one or two lines of text and I don't want them unnecessarily tall.</p> <p>I tried many of the solutions in this thread, and the one that worked for me was the 'hackish suggestion' involving cloned elements suggested by Jonathan Sampson. I translated his idea into the following code. Please feel free to suggest improvements.</p> <p>The functions are delegated to a parent element to handle div's created via an Ajax call. The <code>div.overflow_expandable</code> class has the following declaration: <code>{ max-height: 5em; overflow: hidden; }</code></p> <pre><code>$('#results').delegate('div.overflow_expandable', 'mouseenter', function() { var $this = $(this); // Close any other open divs $('#results div.overflow_expandable').not($(this)).trigger('mouseleave'); // We need to convert the div's current natural height (which is less than // or equal to its CSS max-height) to be a defined CSS 'height' property, // which can then animate; and we unset max-height so that it doesn't // prevent the div from growing taller. if (!$this.data('originalHeight')) { $this.data('originalHeight', $this.height()); $this.data('originalMaxHeight', parseInt($this.css('max-height'))); $this.css({ 'max-height':'none', height: $this.data('originalHeight') }); } // Now slide out if the div is at its original height // (i.e. in 'closed' state) and if its original height was equal to // its original 'max-height' (so when closed, it had overflow clipped) if ($this.height() == $this.data('originalHeight') &amp;&amp; $this.data('originalMaxHeight') == $this.data('originalHeight')) { // To figure out the new height, clone the original element and set // its height to auto, then measure the cloned element's new height; // then animate our div to that height var $clone = $this.clone().css({ height: 'auto', position: 'absolute', zIndex: '-9999', left: '-9999px', width: $this.width() }) .appendTo($this); $this.animate({ height: $clone.height() }, 'slow'); $clone.detach(); } }).delegate('div.overflow_expandable', 'mouseleave', function() { var $this = $(this); // If the div has 'originalHeight' defined (it's been opened before) and // if it's current height is greater than 'originalHeight' (it's open // now), slide it back to its original height if ($this.data('originalHeight') &amp;&amp; $this.height() &gt; $this.data('originalHeight')) $this.animate({ height: $this.data('originalHeight') }, 'slow'); }); </code></pre>
    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.
    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