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'); }); });
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.
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.
jQuery Animations - The animate() Method The jQuery animate() method is used to create custom animations. Syntax: $(selector). animate({params},speed,callback);
Give this a try:
$(document).ready(function(){ $("#topbar-show").toggle(function(){ $(this).animate({height:40},200); },function(){ $(this).animate({height:10},200); }); });
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