How to Animate an Element to Its Auto Height in jQuery
When attempting to animate a
The provided code:
$("div:first").click(function(){ $("#first").animate({ height: "auto" }, 1000 ); });
encounters issues because browsers will not animate a change in height from a fixed value to "auto."
To achieve the desired animation, follow these steps:
var curHeight = $('#first').height();
$('#first').css('height', 'auto');
var autoHeight = $('#first').height();
$('#first').height(curHeight).animate({height: autoHeight}, 1000);
This solution works because it first retrieves the current height, allowing the browser to determine its final height when set to "auto."
The above is the detailed content of How to Animate a Div from a Fixed Height to Its Auto Height in jQuery?. For more information, please follow other related articles on the PHP Chinese website!