jQuery - slideToggle, testing if up or down

jQuery - slideToggle, testing if up or down

Hi all

I have a simple slideToggle that slides a sub menu up and down


$('a#btn-portfolio').click(function(){
  $('#sub_menu').slideToggle('slow');
});

I also have a .scroll function when the window is scrolled.

If the window is scrolled and the #sub_menu is closed I would like #sub_menu to slide down.

Is there a way of testing if the #sub_menu is open or closed when using slideToggle.

Something like:


if(!#sub_menu.open){
  #sub_menu.slideDown();
}

It’s actually pretty simple, if you search the jQuery website for a method called is it will explain the pseudo values it accepts. For what you want you would use .is(‘:hidden’) which when attached to an element returns true or false.

SgtLegend, thanks for your reply

I did look on the jQuery site but never saw anything about pseudo values

.slideToggle() – jQuery API

So would it look like this


if(#sub_menu.is(':hidden')){
  #sub_menu.slideDown();
}


Almost, the id selector needs to be wrapped in the jQuery selector function.

if ($('#sub_menu').is(':hidden')) {
    $('#sub_menu').slideDown();
}