I'll add one, just to have the efficiency argument in there:
$(function(){
function animateHeight($item,ht,spd){
$item.stop().animate({height:ht},spd);
}
$("#topbar-show").click(function(){
$(this).toggle(function(){
animateHeight($(this),40,200);
},function(){
animateHeight($(this),10,200);
});
});
});
I added the .stop()
method to prevent queuing of animation, and made a function out of the animation, which allows the flexibility to use the animation on as many objects as you like.
If the .toggle()
function isn't to your liking, you could always use a class instead:
$(function(){
function animateHeight($item,ht,spd){
$item.stop().animate({height:ht},spd);
}
$("#topbar-show").click(function(){
if($(this).hasClass('clicked')){
$(this).removeClass('clicked');
animateHeight($(this),10,200);
} else {
$(this).addClass('clicked');
animateHeight($(this),40,200);
}
});
});
I personally prefer the class method, but to each their own.