简体   繁体   中英

How To open Sub Menu with onclick Function and hide with onclick Function

I made a sidebar nav I did this so on mouseover opens sub menu but I want it on click to open and onclick on same tab to hide it. Please check my code in this codepen Thanks.

<nav class="navigation">
  <ul class="mainmenu">
    <li><a href="">Home</a></li>
    <li><a href="">About</a></li>
    <li><a href="">Products</a>
      <ul class="submenu">
        <li><a href="">Tops</a></li>
        <li><a href="">Bottoms</a></li>
        <li><a href="">Footwear</a></li>
      </ul>
    </li>
      <li><a href="">Contact us</a></li>
  </ul>
</nav>

https://codepen.io/anon/pen/erNpYG

Thanks In Advance.

You can use a hidden checkbox with the general sibling combinator ( ~ ) to close and open the sub menu on click without javascript:

 html, body { font-family: Arial, Helvetica, sans-serif; } .navigation { width: 300px; } .mainmenu, .submenu { list-style: none; padding: 0; margin: 0; } .mainmenu a, .mainmenu label { display: block; background-color: #CCC; text-decoration: none; padding: 10px; color: #000; } .mainmenu a:hover, .mainmenu label:hover { background-color: #C5C5C5; } /* hide the input */ .mainmenu input { display: none; } /* if a sibling checkbox is check show the menu */ .mainmenu input:checked ~ .submenu { display: block; max-height: 200px; } .submenu a, .submenu label { background-color: #999; } .submenu a:hover { background-color: #666; } .submenu { overflow: hidden; max-height: 0; transition: all 0.5s ease-out; } 
 <nav class="navigation"> <ul class="mainmenu"> <li><a href="">Home</a></li> <li><a href="">About</a></li> <li><input type="checkbox" id="products"> <label for="products">Products</label> <ul class="submenu"> <li><a href="">Tops</a></li> <li><a href="">Bottoms</a></li> <li><a href="">Footwear</a></li> </ul> </li> <li><a href="">Contact us</a></li> </ul> </nav> 

I hope this helps you out. I have added some jquery. Used Id for the target submenu. You can repeat it for others too. Just need to change id. I have created one example for about tab too. here on click of another tab the other tab menu will shut down.

 $('.dropdown').click(function () { var target_id = $(this).attr('data-toggle'); $('.dropdown-list').not(target_id).slideUp(); $(target_id).slideToggle(); $('.dropdown-list').not(target_id).parents('li, .icon-tab').removeClass('active'); $('.dropdown').not(this).parents(' .icon-tab').removeClass('active'); $(this).parents('li, .icon-tab').toggleClass('active'); }); 
 html, body { font-family: Arial, Helvetica, sans-serif; } /* define a fixed width for the entire menu */ .navigation { width: 300px; } /* reset our lists to remove bullet points and padding */ .mainmenu, .submenu { list-style: none; padding: 0; margin: 0; } /* make ALL links (main and submenu) have padding and background color */ .mainmenu a { display: block; background-color: #CCC; text-decoration: none; padding: 10px; color: #000; } /* add hover behaviour */ .mainmenu a:hover { background-color: #C5C5C5; } /* when hovering over a .mainmenu item, display the submenu inside it. we're changing the submenu's max-height from 0 to 200px; */ /* we now overwrite the background-color for .submenu links only. CSS reads down the page, so code at the bottom will overwrite the code at the top. */ .submenu a { background-color: #999; } /* hover behaviour for links inside .submenu */ .submenu a:hover { background-color: #666; } /* this is the initial state of all submenus. we set it to max-height: 0, and hide the overflowed content. */ .submenu { display:none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav class="navigation"> <ul class="mainmenu"> <li><a href="">Home</a></li> <li><a class="dropdown" data-toggle="#about">About</a> <ul class="submenu dropdown-list" id="about"> <li><a href="">Tops</a></li> <li><a href="">Bottoms</a></li> <li><a href="">Footwear</a></li> </ul> </li> <li><a class="dropdown" data-toggle="#products">Products</a> <ul class="submenu dropdown-list" id="products"> <li><a href="">Tops</a></li> <li><a href="">Bottoms</a></li> <li><a href="">Footwear</a></li> </ul> </li> <li><a href="">Contact us</a></li> </ul> </nav> 

try this using js

 $(document).ready(function(){ $('.dropdown').each(function() { var $dropdown = $(this); $(".dropdown-link", $dropdown).click(function(e) { e.preventDefault(); $div = $(".submenu", $dropdown); $div.toggle(); $(".submenu").not($div).hide(); return false; }); }); $('html').click(function(){ $(".submenu").hide(); }); }); 
 html, body { font-family: Arial, Helvetica, sans-serif; } /* define a fixed width for the entire menu */ .navigation { width: 300px; } /* reset our lists to remove bullet points and padding */ .mainmenu, .submenu { list-style: none; padding: 0; margin: 0; } /* make ALL links (main and submenu) have padding and background color */ .mainmenu a { display: block; background-color: #CCC; text-decoration: none; padding: 10px; color: #000; } /* add hover behaviour */ .mainmenu a:hover { background-color: #C5C5C5; } /* when hovering over a .mainmenu item, display the submenu inside it. we're changing the submenu's max-height from 0 to 200px; */ /* we now overwrite the background-color for .submenu links only. CSS reads down the page, so code at the bottom will overwrite the code at the top. */ .submenu a { background-color: #999; } /* hover behaviour for links inside .submenu */ .submenu a:hover { background-color: #666; } /* this is the initial state of all submenus. we set it to max-height: 0, and hide the overflowed content. */ .submenu { overflow: hidden; display: none; -webkit-transition: all 0.5s ease-out; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav class="navigation"> <ul class="mainmenu"> <li><a href="">Home</a></li> <li class="dropdown"> <a href="#" class="dropdown-link">About</a> <ul class="submenu"> <li><a href="">Tops</a></li> <li><a href="">Bottoms</a></li> <li><a href="">Footwear</a></li> </ul> </li> <li class="dropdown"> <a href="#" class="dropdown-link">Products</a> <ul class="submenu"> <li><a href="">Tops</a></li> <li><a href="">Bottoms</a></li> <li><a href="">Footwear</a></li> </ul> </li> <li class="dropdown"> <a href="#" class="dropdown-link">Other dropdown</a> <ul class="submenu"> <li><a href="">Tops</a></li> <li><a href="">Bottoms</a></li> <li><a href="">Footwear</a></li> </ul> </li> <li><a href="">Contact us</a></li> </ul> </nav> 

This is using pure JS

CSS

.subrollin{
  overflow: hidden;
  max-height: 0;
  -webkit-transition: all 0.5s ease-in;
}
.subrollout{
  overflow: block;
  max-height: 200px;
  -webkit-transition: all 0.5s ease-out;
}

JS

var element = document.getElementsByClassName('parentmenu')[0];
element.addEventListener("click", function(e) {
var sub = document.getElementsByClassName('submenu')[0];
  if(sub.classList.contains('subrollout')){
      sub.classList.add("subrollin");    
      sub.classList.remove("subrollout");
  } else {
      sub.classList.add("subrollout");
      sub.classList.remove("subrollin");
  }
}, false);

see this codepen : https://codepen.io/anon/pen/gzpaer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM