Here's a quick exercise to find out if there is a more readable way. If you use something like rel , it will be clear that for JS, unlike CSS styles. It hides content with a whole height of your choice and targets any selector you want. For each element that you want this to happen ... It will capture its natural height and store it in the data attribute of the element. Then, when you click the read more button, it will occupy that height and use it for animation.
http://codepen.io/sheriffderek/pen/rLYdky
Markup
<article rel='reveal'> <h2>Thing 1</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugiat quaerat molestiae voluptas soluta officia veniam ad, alias fuga sit, adipisci, similique harum odio eos vel. Et, numquam, error. Doloribus, ipsa!</p> ... <button>More</button> </article>
Javascript
(function() { // closure for this program // so nothing leaks... var hideContent = function(element, maxHeight) { $('[rel="reveal"]').each( function() { var naturalHeight = $(this).outerHeight(); $(this).data('height', naturalHeight); $(this).css('height', maxHeight); }); }; $('[rel="reveal"]').on('click', function() { var naturalHeight = $(this).data('height'); var speed = naturalHeight*2; // relative to height maybe? $(this).find('button').fadeOut(); $(this).animate({ 'height': naturalHeight }, speed); }); hideContent('[rel="reveal"]', 200); })();
source share