Skip to content

Instantly share code, notes, and snippets.

@JoanClaret
Last active August 29, 2015 14:14
Show Gist options
  • Save JoanClaret/856fec9b3838aead4763 to your computer and use it in GitHub Desktop.
Save JoanClaret/856fec9b3838aead4763 to your computer and use it in GitHub Desktop.
Animate to an unknown height with jquery
$(document).ready(function(){
$('.js-show').click(function(){
var id = $(this).attr('id');
var el = $('.' + id);
if ($(this).hasClass('open')){
el.animate({height: '46px'}, 600);
}
else{
curHeight = el.height();
autoHeight = el.css('height', 'auto').height();
el.height(curHeight).animate({height: autoHeight}, 600, function(){
el.height('auto'); // set height to auto for responsive sites
});
}
$(this).toggleClass('open');
});
});
@JoanClaret
Copy link
Author

@JoanClaret
Copy link
Author

... or simply use Max-height instead of height : )

http://jsfiddle.net/thechrisjordan/3Fc7D/23/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment