Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - animate height toggle

I have a 10px bar along the top of the screen that, when clicked, I want it to animate to a height of 40px and then if clicked again, animate back down to 10px. I tried changing the id of the div, but it isn't working. How could I get this to work, or is there a better way to do it?

body html:

<div id="topbar-show"></div>

css:

#topbar-show { width: 100%; height: 10px; background-color: #000; } #topbar-hide { width: 100%; height: 40px; background-color: #000; } 

javascript:

$(document).ready(function(){   $("#topbar-show").click(function(){     $(this).animate({height:40},200).attr('id', 'topbar-hide');   });   $("#topbar-hide").click(function(){     $(this).animate({height:10},200).attr('id', 'topbar-show');   }); }); 
like image 971
We're All Scholars Avatar asked Feb 11 '11 02:02

We're All Scholars


People also ask

Which jQuery method can be used to gradually change the height of a selected element?

Definition and Usage. The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect.

How does jQuery slideToggle work?

jQuery slideToggle() Method The slideToggle() method toggles between slideUp() and slideDown() for the selected elements. This method checks the selected elements for visibility. slideDown() is run if an element is hidden. slideUp() is run if an element is visible - This creates a toggle effect.

What is the correct syntax of animate () method?

jQuery Animations - The animate() Method The jQuery animate() method is used to create custom animations. Syntax: $(selector). animate({params},speed,callback);


1 Answers

Give this a try:

$(document).ready(function(){   $("#topbar-show").toggle(function(){     $(this).animate({height:40},200);   },function(){     $(this).animate({height:10},200);   }); }); 
like image 104
Ian Christian Myers Avatar answered Sep 25 '22 08:09

Ian Christian Myers