Animate css width or height to auto in JQuery
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.
Jesus Hates the Yankees
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?
About Me
I'm a designer, developer, and teacher based in Harrisburg, Pa. I run Hauck Interactive, Inc.
Categories
Archives
- January 2012
- December 2011
- October 2011
- August 2011
- July 2011
- June 2011
- May 2011
- March 2011
- February 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005


