Question

Hi I am using the menu structure below for a responsive website, with some help I have managed to get the sub menu's to open and close any menu items already open on the two sub menu levels.

I am trying to add toggleClass('open') into my script but I can't figure out where it needs to go so that the class toggles when a menu item is opened and closed by clicking the header and also toggles back to .closed when another menu item is opened. I can get it to toggle when the headers are clicked on by adding $(this).toggleClass('open');to the top of the function but it doesn't toggle again if you open a different menu item.

Html:

<nav id="main-nav" role="navigation">
        <div class="block">
            <h2 class="block-title">Main menu</h2>
            <ul>
                <li class="is-active">
                    <a href="#">Link</a>
                </li><!--
             --><li>
                    <a class="toggle-sub-nav closed" href="#">Link ></a>
                      <ul class="sub-nav">
                          <li><a href="#">Sub link</a></li>
                          <li><a href="#">Sub link</a></li>
                          <li><a href="#">Sub link</a></li>
                      </ul>
                </li><!--
             --><li>
                    <a href="#">Link</a>
                </li><!--
             --><li>
                    <a class="toggle-sub-nav closed" href="#">Link ></a>
                    <ul class="sub-nav">
                          <li><a href="#">Sub link</a></li>
                          <li> <a class="toggle-sub-nav closed" href="#">Sub Link ></a>
                    <ul class="sub-nav">
                          <li><a href="#">Sub link</a></li>
                          <li><a href="#">Sub link</a></li>
                          <li><a href="#">Sub link</a></li>
                      </ul></li>
                          <li><a href="#">Sub link</a></li>
                      </ul>
                </li><!--
             --><li>
                    <a href="#">Link</a>
                </li>
            </ul>
           </div>
</nav>

Sub menu css:

.sub-nav { 

     height: 0px;
    -webkit-transition: all 600ms cubic-bezier(.42,0,.58,1);
    -moz-transition: all 600ms cubic-bezier(.42,0,.58,1);
    -o-transition: all 600ms cubic-bezier(.42,0,.58,1);
    -ms-transition: all 600ms cubic-bezier(.42,0,.58,1);
    transition: all 600ms cubic-bezier(.42,0,.58,1);
    overflow:hidden;

  }


  .sub-nav-open {

    height: auto;
    max-height: 500px;

  }

.sub-nav li { background-color: #666; } 

.closed { background: url(../assets/img/arrows.png) no-repeat 90% -24px; }

.open { background: url(../assets/img/arrows.png) no-repeat 90% 17px; }

Jquery:

$(function () {
    $(".toggle-sub-nav").click(function () {
        if ($(this).siblings('ul').hasClass('sub-nav-open')) {
            $(this).siblings('ul').find('sub-nav-open').andSelf().removeClass('sub-nav-open')
        } else {
            $('.sub-nav').not($(this).closest('.sub-nav')).removeClass('sub-nav-open');
            $(this).next(".sub-nav").toggleClass('sub-nav-open');
        }
        return false;
    });
});
Was it helpful?

Solution

After a lot of playing around I have modified the jquery and have it working how I requested, the only issue I have left is when you open a 2nd level sub menu is resets the arrow image on the top level. I am really not sure how to accomplish that so any ideas appreciated or if any of you have a cleaner solution I would love to hear it?:

Here is the modified function:

$(function () {
    $(".toggle-sub-nav").click(function () {
        if ($(this).siblings('ul').hasClass('sub-nav-open')) {
            $(this).removeClass('open').siblings('ul').find('sub-nav-open').andSelf().removeClass('sub-nav-open')
        } else {
     $('.toggle-sub-nav').not($(this).closest('.toggle-sub-nav')).removeClass('open');
            $('.sub-nav').not($(this).closest('.sub-nav')).removeClass('sub-nav-open');
            $(this).toggleClass('open').next(".sub-nav").toggleClass('sub-nav-open');
        }
        return false;
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top