28

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!

Kevin Brown
  • 12,602
  • 34
  • 95
  • 155

4 Answers4

54
$('.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);
});
Aldi Unanto
  • 3,626
  • 1
  • 22
  • 30
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • 5
    This function is deprecated as of JQuery 1.8 and removed in JQuery 1.9. http://api.jquery.com/toggle-event/#entry-longdesc – Xtrinity Dec 31 '15 at 09:22
6

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);
 });
Fabrizio
  • 7,603
  • 6
  • 44
  • 104
John Darling
  • 99
  • 1
  • 4
  • 1
    so, what are you trying to say ? i dont think it is helpful to give code that dosent work and say "Oh, it dosent work". post a real solution. – Rafael Herscovici Feb 14 '13 at 10:26
  • I think it is helpful to give code that dosent work and say "Oh, it dosent work". – Alireza Dec 21 '13 at 09:17
  • 1
    This function is deprecated as of JQuery 1.8 and removed in JQuery 1.9. http://api.jquery.com/toggle-event/#entry-longdesc – Xtrinity Dec 31 '15 at 09:23
0

Use toggle() or slideToggle from jquery:

Toggle

  $( "#clickme" ).click(function() {
      $( "#book" ).slideToggle( "slow", function() {
        // Animation complete.
      });
    });


 <div id="clickme">
      Click here
    </div>
    <img id="book" src="book.png" alt="" width="100" height="123">
Maizied Hasan Majumder
  • 1,197
  • 1
  • 12
  • 25
-8
onmouseover="$('.play-detail').stop().animate({'height': '84px'},'300');" 

onmouseout="$('.play-detail').stop().animate({'height': '44px'},'300');"

Just put two stops -- one onmouseover and one onmouseout.

Conner
  • 30,144
  • 8
  • 52
  • 73
will
  • 33
  • 2
    Using inline JavaScript is not a good pattern. If you are already using jQuery use event handlers instead. – akluth Oct 26 '12 at 08:11