Posted in jquery
11
3:52 am, June 15, 2021

jquery toggle menu testing

using jquery toggle to show and hide the sub menu's

HTML

<div id="headermenu" class="click-nav">
  <ul class="no-js">
    <li id=""><a href="#!">menu1</a></li>
    <li id=""><a class="have-second-level-menu" href="#!">menu2</a>
      <ul class="sub-menu">
        <li ><a class="have-third-level-menu" href="#!">submenu1></a>
          <ul>
            <li><a href="#!">submenu.1 </a></li>
            <li><a href="#!">submenu.2</a></li>
            <li><a href="#!">submenu.3</a></li>
            <li><a href="#!">submenu.4</a></li>
          </ul>
        </li>
        <li><a href="#!">submenu4</a></li>
        <li ><a class="have-third-level-menu" href="#!">submenu1</a></li>
        <li><a href="#!">submenu7</a></li>
      </ul>
    </li>
  </ul>
</div>

CSS

#headermenu {
    width: 90%;
    height: 20%;
    margin-right: 5%;
    margin-left: 5%;
    margin-bottom: .5%;
}
#headermenu ul ul {
    display: none;
}
/*#headermenu ul li:hover > ul {
        display: block !important;
    }*/
#headermenu ul {
    padding: 0;
    margin: 0;
    white-space: nowrap;
    list-style: none;
    position: relative;
    display: inline-table;
}
#headermenu ul li {
    width: 100px;
    height: 45px;
    float: left;
    left: 0;
    text-decoration: none;
    text-align: center;
    font-family: century gothic;
    background-color: #fef3e2;
}
#headermenu ul li:hover {
    background: #feaa38;
}
#headermenu ul li:hover a {
    color: #ffffff;
}
#headermenu ul li a {
    color: #feaa38;
    display: block;
    text-decoration: none;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}
#headermenu ul ul {
    border-radius: 0px;
    padding: 0;
    position: absolute;
    top: 100%;
}
#headermenu ul ul li {
    float: none;
    background: #b0c7bd;
    position: relative;
}
#headermenu ul ul li:hover {
    background: #aea7a0;
}
#headermenu ul ul li a {
    color: #ffffff;
}
#headermenu ul ul li a:hover {
    background: #aea7a0;
}
#headermenu ul ul ul {
    position: absolute;
    left: 100%;
    top: 0;
}
#headermenu:active ul ul {
    display: none;
}

Javascript

$(function () {
  $('.click-nav .no-js .have-second-level-menu').click(function(e) {
     $(this).closest('li').find('.sub-menu').slideToggle(200);
    $('.clicker').toggleClass('active');
    e.stopPropagation();
  });

  $('.have-third-level-menu').click(function(e) {
      $(this).closest('li').find('ul').slideToggle(200);
    $('.clicker').toggleClass('active');
    e.stopPropagation();
  });
});

External Link for jquery toggle menu testing

View Statistics
This Week
27
This Month
96
This Year
0

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

Join me on Substack if you want me to send you a collection of the things i have done or found or read for the week. Or follow me on twitter if you prefer, i dont post much but i probably should!

❤👩‍💻🕹

Random Quote
When I realized that, no individual step is hard in any process. Building this airport I'm standing in right now started with a guy writing the architectural plans on paper. That's not hard for him to do. Then laying the first beam isn't had. The whole thing is really hard. So, just take each step kind of piece by piece and when I was able to do that and stop trying to chase this prize and started putting in the work, things just started coming together.
Unknown