SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jan 2009
    Posts
    383
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    Code:
    $('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:

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

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2009
    Posts
    383
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

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

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Almost, the id selector needs to be wrapped in the jQuery selector function.

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •