Say I need an element to animate when clicked. To do this I just:
$('#header .icon').click(function() {
$('#header form').animate({width:'195px'});
});
But how would I make it so that when I click the element again the opposite animate takes place? (e.g. animate to width: 0px; )
You can use .toggle() like this:
$('#header .icon').toggle(function() {
$('#header form').animate({width:'195px'});
}, function() {
$('#header form').animate({width:'0px'});
});
If it was initially out you could toggle it's width like this:
$('#header .icon').click(function() {
$('#header form').animate({width:'toggle'});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With