Last active
August 29, 2015 14:14
-
-
Save JoanClaret/856fec9b3838aead4763 to your computer and use it in GitHub Desktop.
Animate to an unknown height with jquery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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'); | |
}); | |
}); |
... or simply use Max-height instead of height : )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DEMO: http://codepen.io/joanclaret/pen/KwyKzN