Note
An improved solution can be found in this post.
Source code and demo
One common CSS3 Transition is to slide up (collapse) and slide down (expand) a box by manipulating its "height" attribute, e.g. changing 400px to 0. However, when either height is set to "auto", the transition won't work anymore. This topic has been discussed here.
The solution is to change the "max-height" instead of "height". Max-height is a CSS attribute supported in almost all modern browsers (see compatibility chart here). It defines the maximum height of an element. We can use it to "shrink" a box by setting max-height to 0, or expand a box by restoring its original height. In order to restore the original height, we need to retain the computed height of the box content.
Adam at stackoverflow.com provided a solution inspired by this same idea. I simplified the solution by removing some of the JavaScript code.
HTML markup
Here I create an item (div.item) with a title (<h2>) and content area (div.content). I want to make the content area slide down (expand) or slide up (collapse) once the item is clicked. The title will always be visible.
<div class="item"> <!-- Title --> <h2>Click me to expand</h2> <!-- Content wrapper --> <div class="content_w"> <!-- Content --> <div class="content"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ... </div> </div> </div>
I put the content div inside a wrapper (div.content_w). Instead of changing the max-height of the real content div, we change the wrapper's. This way, we can achieve the sliding up/down effect while still retaining the computed height of the real content div.
Style sheet
Here is the style sheet. Please notice that the max-height and transition are set on the content wrapper rather than the content itself. The content will remain unchanged no matter if the wrapper is collapsed or expanded. The content wrapper needs to have "overflow: hidden" in order to hide its contained content when the wrappers's height becomes less than the content's.
.item { width: 400px; } /* Content wrapper */ .content_w { overflow: hidden; max-height: 0; -webkit-transition: max-height 0.5s; -moz-transition: max-height 0.5s; -o-transition: max-height 0.5s; transition: max-height 0.5s; }
JavaScript
With a little help from jQuery, I toggle the max-height between 0 and the content height based on the "open" class which I use simply to mark the expanded and collapsed state.
(function($) { // max-height transition. // Inspired by http://jsfiddle.net/adambiggs/MAbD3/ function toggleContent($contentWrapper) { // Get the computed height of the content var contentHeight = $('.content', $contentWrapper).outerHeight(true); // Add or remove class "open" $contentWrapper.toggleClass('open'); // Set max-height if ($contentWrapper.hasClass('open')) { $contentWrapper.css('max-height', contentHeight); } else { $contentWrapper.css('max-height', 0); } } // Listen to click events on the item element $('.item').on('click', function(e) { e.preventDefault(); toggleContent($('.content_w', this)); }); })(jQuery);
One thing to notice is that I didn't use jQuery to do the transition. The transition is done by CSS. jQuery is used only for selecting DOM elements, marking elements, and applying CSS styles. You can replace jQuery with any of your favorite JavaScript libraries.
Limitations
The max-height is set to the content height when it is expanded. So if the content changes or re-flows later, some content will be clipped. The extra code that Adam put there is to prevent this by setting max-height to a really big number at the end of the expand transition. However, if you need a simple slideup box whose content and layout won't change after expansion, then this solution should work fine for you.
An improved solution can be found in this post.
What about detecting if the document is resized? You can then recalculate the max height after the resize and adjust from there.
ReplyDelete