Reputation: 12568
I am using the follow jsfiddle to slide a div down when an element is clicked...
$(document).ready(function() {
// Initial variables
var panel = $('#panel');
var panelHeight = panel.height();
// Set the height to 0
panel.height(0);
// Animate it to its initial size
$('a').click(function(e) {
e.preventDefault();
$('#panel').animate({'height' : panelHeight});
});
});
I can't work out how to made it disappear again when the element is clicked once more. Can anyone help?
Upvotes: 0
Views: 33
Reputation: 367
Best Solution is to set the #panel div to display:none
and then slideToggle
it...
$('a').click(function(e) {
$('#panel').slideToggle();
});
Upvotes: 1
Reputation: 10021
I offer you an alternative:
set the #panel
to display: none;
then you can use slideToggle()
and you can keep that sliding effect: Demo
if you'd like to continue using your way with animate
, I've updated your fiddle so it works: Demo
Upvotes: 1
Reputation: 1621
Better you can use jquery toggle. http://api.jquery.com/toggle/
$('a').click(function(e) {
e.preventDefault();
$('#panel').toggle();
});
No need to keep height as '0' and original height everytime.
Upvotes: -1