Skip to content

Instantly share code, notes, and snippets.

@shomanishikawa
Last active December 31, 2015 03:29
Show Gist options
  • Save shomanishikawa/7927674 to your computer and use it in GitHub Desktop.
Save shomanishikawa/7927674 to your computer and use it in GitHub Desktop.
jQuery animate to auto height/width
jQuery.fn.animateAuto = function(prop, speed, callback){
var elem, height, width;
return this.each(function(i, el){
el = jQuery(el), elem = el.clone().css({"height":"auto","width":"auto"}).appendTo(el.parent());
height = elem.css("height"),
width = elem.css("width"),
elem.remove();
if(prop === "height")
el.animate({"height":height}, speed, callback);
else if(prop === "width")
el.animate({"width":width}, speed, callback);
else if(prop === "both")
el.animate({"width":width,"height":height}, speed, callback);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment