views:

46

answers:

3

I'm trying to use jQuery's slideToggle function to show or hide a panel which has been hidden using CSS position, rather than display:none (as this causes issues with a Google Map in one of my panels).

At the moment I'm just hiding and showing the panels like so, but some animation would be nice:

$('.panel').addClass('hidden');
$('.head > span').addClass('closed');

$('.head').click(function() { 
    $(this).next('.panel').toggleClass('hidden');
    $(this).children('span').toggleClass('open');
});

Any ideas?

+1  A: 

slideToggle() is an alternative to toggle(), which shows/hides the selected items depending on its current visible state.

You needn't worry about the classes at all if you are simply trying to get the animation to work.

So, just try the following to see what you get:

$(this).next('.panel').slideToggle();
James Wiseman
@James, I beleive that slideToggle will not work if OP has done CSS positioning to hide element.
VinayC
SlideToggle is a really cool jquery function, i'm with James. I take the liberty to add the stop instructions to the one given by James: $(this).next('.panel').stop(true,true.)slideToggle(); in order to manage impulsive clicks performed by the user
Marcello Faga
@VinayC : your warning is right IMO, some css properties will not be compatible with the slidetoggle function, like hiding and dimensioning the control. If you put height:100% in your sliding panel, the slidingn effect will be brutally ruined by this value
Marcello Faga
A: 
JapanPro
+1  A: 

slideToggle animates height of the element in question apart from visibility. Not sure how exactly you have used CSS position to show/hide your panels. Based on that you have to build you own animation using animate function. Another quick way could be to

For showing element:

  1. Hide the element (using jquery hide())

  2. Apply your class to show element (i.e. to adjust its position)

  3. Now apply slideDown

For hiding content:

  1. Apply slideUp - use callback function to do steps 2 & 3

  2. Apply your class to hide element (i.e. to adjust its position outside window)

  3. Show the element (using jquery show())

Edit: Illustrative code goes below (assuming that 'hidden' classes will do CSS positioning to hide the element):

function customSlideToggle(e)
{
   var show = e.hasClass('hidden');
   if (show) {
     e.hide();
     e.removeClass('hidden')
     e.slideDown('slow');
   }
   else
   {
     e.slideUp('slow', function() {
        e.addclass('hidden');
        e.show();
     });
   }
}
VinayC
Brilliant. That seems to work perfectly - thanks!
Pezholio