Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery toggle animation

I have this jQuery:

$(document).ready(function() {    $("#panel").hide();     $('.login').toggle(    function()    {       $('#panel').animate({       height: "150",        padding:"20px 0",       backgroundColor:'#000000',       opacity:.8 }, 500);    },    function()    {       $('#panel').animate({       height: "0",        padding:"0px 0",       opacity:.2       }, 500);    }); }); 

This is working fine, but I need to extend the functionality a little. I want to also similarly manipulate another div's properties in sync with the #panel div. I tried adding two more functions relating to the secondary div, but I just got a 4-phase toggle...haha! Forgive my ignorance!

Thanks guys!

like image 761
Kevin Brown Avatar asked May 31 '09 02:05

Kevin Brown


People also ask

How do you add transitions to toggle in jQuery?

toggle() changes from display:none to display:block , a property which is not animatable. Use toggleClass() to ...well...toggle :)/change classes on the menu and style those classes. So use left:-100% and left:200px ( if that is where you want to position the menu ) and together with transition or maybe opacity.

How do you toggle slideUp and slideDown in jQuery?

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 toggle () in jQuery?

The toggle() method attaches two or more functions to toggle between for the click event for the selected elements. When clicking on an element, the first specified function fires, when clicking again, the second function fires, and so on. Note: There is also a jQuery Effects method called toggle().

Is jQuery good for animation?

jQuery was not created for animating objects, so it may be slow and laggy (depending on what and how much you're animating). But scrolling a page with jQuery 's . animate() function for example works very good in most cases.


2 Answers

$('.login').toggle(     function(){         $('#panel').animate({             height: "150",              padding:"20px 0",             backgroundColor:'#000000',             opacity:.8         }, 500);         $('#otherdiv').animate({             //otherdiv properties here         }, 500);     },     function(){         $('#panel').animate({             height: "0",              padding:"0px 0",             opacity:.2         }, 500);              $('#otherdiv').animate({             //otherdiv properties here         }, 500); }); 
like image 92
Vincent Ramdhanie Avatar answered Oct 09 '22 05:10

Vincent Ramdhanie


I dont think adding dual functions inside the toggle function works for a registered click event (Unless I'm missing something)

For example:

$('.btnName').click(function() {  top.$('#panel').toggle(function() {    $(this).animate({       // style change    }, 500);    },    function() {    $(this).animate({       // style change back    }, 500);  }); 
like image 29
John Darling Avatar answered Oct 09 '22 05:10

John Darling