Personal and Professional Blog of Rich Hauck

Animate css width or height to auto in JQuery

September 20, 2010

I recently wrapped up a project where we wanted to reveal only a portion of an HTML element and have it fully display on-click (In our case, it was a container that held a number of keyword tags). We wanted something similar to slideToggle(), however, slideToggle() hides and reveals the entire element.

What we needed was a div that could animate() from a fixed size to “auto” (since the div contents would vary in length). Unfortunately, JQuery can’t seem to calculate width:”auto” or height:”auto”. After looking around without an answer, I came up with a solution–place the variable div into a holder div and measure the holder’s properties. Here’s the example; code below.


	$(document).ready(function() {
		var startHeight = $('.box').height();
		$('.btn').click(function() {
		   // find nearby elements
		   var box = $(this).parent().find('.box');
		   var inner = box.children('.inner');

		   // collect current heights
		   var innerH = inner.height();
		   var boxH = box.height();

		   // calculate new height
		   var newHeight = parseInt(boxH) + parseInt(innerH);

		   // determines whether to toggle height
		   newHeight = boxH > startHeight ? startHeight : newHeight;
		   box.animate({height:newHeight});
		});
	});

You’ll notice I’m setting a variable startHeight at the beginning of the script to remember the starting CSS property. This can then be referenced by the button click function to revert the size back. Here’s the CSS:

.inner {background:#ccc;height:auto;padding:10px;overflow:hidden;}
.inner ol{margin:0;}
.box{height:30px;overflow:hidden;}

Because I’m dealing with multiple result sets on the same page, I made all of the CSS into class rather than id selectors. That’s also why I’m using this relative selector in Javascript: $(this).parent().find(‘.box’);
Feel free to use, and comment if there’s something that can be improved upon here.

Categories: twitter post, webdesign
Tags: , , , ,

Jesus Hates the Yankees

September 14, 2010

I’m a sucker for stadium giveaways–have been since I was a kid. Since I bought a ticket package for Baltimore I made it a point to collect the set of bobbleheads the Orioles were giving away over the course of the season.

My second son decided to throw a wrench into the plans by being born right before the last giveaway, so I was pretty much forced to turn to eBay. The seller turned out to be very slow in shipping the bobblehead, so as a consolation price he included this shirt:

Now, anyone who knows me knows that, despite liking the Orioles since childhood, I’m anything but a Yankees hater (I’m not that removed from NY life, and I can thank the captain and the other bombers for rekindling my interest in the sport). Furthermore. I’m not really into team bashing . . .

Anyone have thoughts on what I should do with the shirt?

Categories: sports, twitter post
Tags: , ,

About Me

Rich HauckI'm a designer, developer, and teacher based in Harrisburg, Pa. I run Hauck Interactive, Inc.




Archives