Rephrasing this, I asked it poorly before. Hopefully this explains my goal a bit better and if anyone can help I'll be so grateful:
I have two divs set up such that when you click one, the other expands to a height appropriate for its content. The expanding div loads at height:0;overflow:hidden
but since the animation requires an actual value instead of height:auto
, the height is temporarily switched to 'auto', measured, stored in a variable for use in the animation, and then switched back to zero.
The problem is that the expanding div contains content that the user can choose to view or not view, and if the user views longer content, it gets cut off because the parent div has been set at that pixel value height.
So I would like the expanding div's height to be set back to 'auto' after it expands. Then if the user clicks the button again to close the div, the script will re-measure the height and change it to whatever the current pixel value is so that it can be animated back to zero.
Basically this would be the sequence of events:
- Div #2 loads at
height:0
and this height is stored in a variable. - Div's height is set to auto.
- The auto height is stored as a pixel value.
- The height is set back to zero.
- User clicks div #1 to open div #2.
- Div #2's height animates from zero to the pixel height value.
- Its height is set to 'auto' again once the animation is complete.
- User views or hides content, which changes the height of the div.
- User clicks div #1 to close div #2.
- Div #2's current height is measured and stored as another pixel value.
- Div #2's height is set to the new pixel value.
- Div #2's height is animated back to zero.
Using Box9's brilliant post, I've set up the following code, which successfully accomplishes steps 1 through 6. Step 7 is the important one that I can't figure out, but I'm looking for help with steps 7 through 11.
$(function(){
var el = $('#ev-donate-form'),
curHeight = el.height(),
autoHeight = el.css('height', 'auto').height();
el.height(curHeight);
$( "#ev-sponsors" ).click(function() {
if(el.height() == 0)
el.height(curHeight).animate({height: autoHeight}, 600);
else
el.height(autoHeight).animate({height: curHeight}, 600);
});
});
I haven't gotten anything to work. I'm thinking maybe to use some kind of setTimeout()
attached to the click function that would set the height to auto after the duration of the animation? None of my attempts at that have been successful, but I'm not very experienced with jQuery and it seems like that would work...
Here's a fiddle for experimentation: https://jsfiddle.net/q00bdngk/1/