点击下拉菜单

Drop down menu by click

对于我的汉堡包菜单,我正在尝试构建它,以便通过单击将其变成下拉菜单。例如,如果您按下项目 1 旁边的加号,则项目 1 的子项目将下拉。我已将所有项目和子项目添加到汉堡菜单中。但是,我无法弄清楚如何让子项仅在您按下父项旁边的加号时出现。有人有解决办法吗?

jsfiddle - https://jsfiddle.net/yz5p1czh/

jQuery(document).ready(function() {
  jQuery('#hamburger-nav-icon').click(function() {
    jQuery(this).toggleClass('open');
      jQuery('body').css('overflow','hidden');  
    if (jQuery('#hamburger-menu-cover').css('opacity') == '0') {
      jQuery('#hamburger-menu-cover').css('opacity', '1');
      jQuery('#hamburger-menu-cover').fadeIn(300).css('display', 'table'); 
        jQuery('body').css('overflow','hidden');
    } else {
      jQuery('#hamburger-menu-cover').css('opacity', '0');
      jQuery('#hamburger-menu-cover').fadeOut(300).css('display', 'none');
        jQuery('body').css('overflow','visible');
    }
  });
});
/*--------------------------------------------------------------
## Hamburger Menu
--------------------------------------------------------------*/
#hamburger-menu-cover {
    z-index: 5;
    width: 100%;
    height: 100%;
  background: rgba(255, 255, 255, 0.9);
    position: fixed;
    font-size: 1.5em;
    text-align: center;
    right: 0px;
    top: 0px;
    opacity: 0;
    display: none;
    overflow-y: hidden;
   -webkit-overflow-scrolling: touch;
    overflow: hidden;
}
.hamburger-menu {
background:#fff;
   width: 31%;
    height: 100%;
  border-right: 1px solid #808080;
  position: fixed;
  top: 0px;
}
.hamburger-menu-container {
  margin-left: 73px;
  margin-top: 100px;
  padding-right: 32px;
  padding-bottom: 40px; 
  border-bottom: 1px solid #808080;
}
.hamburger-menu-links li  {
    list-style: none;
      font-family: 'freightlight';
  font-size: 36px;
  display: block;
  line-height: 57px;
    font-style: italic;
    text-transform: uppercase;
    text-decoration: none;
}
.hamburger-menu-links li a {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links li a:hover {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links li a:active {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links li a:visited {
    text-decoration: none;
    color:#000;
}

#hamburger-nav-icon {
    z-index: 20;
  width: 50px;
  height: 35px;
  position: relative;
  margin: 35px 30px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .5s ease-in-out;
  -moz-transition: .5s ease-in-out;
  -o-transition: .5s ease-in-out;
  transition: .5s ease-in-out;
  cursor: pointer;
}

#hamburger-nav-icon span {
  display: block;
  position: absolute;
  height: 5px;
  width: 40px;
  background: #000;
  opacity: 1;
  left: 0;
}

/* Icon 3 */

#hamburger-nav-icon span:nth-child(1) {
  top: 0px;
}

#hamburger-nav-icon span:nth-child(2),#hamburger-nav-icon span:nth-child(3) {
  top: 12px;
}

#hamburger-nav-icon span:nth-child(4) {
  top: 24px;
}

#hamburger-nav-icon.open span:nth-child(1) {
  top: 8px;
  width: 0%;
  left: 50%;
}

#hamburger-nav-icon.open span:nth-child(2) {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

#hamburger-nav-icon.open span:nth-child(3) {
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#hamburger-nav-icon.open span:nth-child(4) {
  top: 8px;
  width: 0%;
  left: 50%;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hamburger-nav-icon">
              <span></span>
              <span></span>
              <span></span>
              <span></span>
            </div>
        <div id="hamburger-menu-cover">
            <div class="hamburger-menu">
                <div class="hamburger-menu-container">
                <div class="hamburger-menu-links">
            <ul>
                <li><a href="#">Item 1<i class="fa fa-plus" aria-hidden="true"></i></a></li>
<div class="submenu">
                     <li><a href="#">Sub Item 1</a></li>
                <li><a href="#">Sub Item 2</a></li>
                    </div>
                <li><a href="#">Item 3</a></li>
                <li><a href="#">Item 4</a></li>
                <li><a href="#">Item 5<i class="fa fa-plus" aria-hidden="true"></i></a></li>
<div class="submenu">
                     <li><a href="#">Sub Item 1</a></li>
                <li><a href="#">Sub Item 2</a></li>
                    </div>
                <li><a href="#">Item 6</a></li>
                <li><a href="#">Item 7</a></li>
                <li><a href="#">Item 8</a></li>
                <li><a href="#">Item 9</a></li>
            </ul>
                    </div>
                </div>
            </div>
</div>

试试这个:https://jsfiddle.net/yz5p1czh/8/

HTML

<div id="hamburger-nav-icon">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>
<div id="hamburger-menu-cover">
<div class="hamburger-menu">
  <div class="hamburger-menu-container">
     <div class="hamburger-menu-links">
       <ul>
          <li><a href="#">Item 1<i class="fa fa-plus menutoggle" id="toggle1" aria-hidden="true"></i></a></li>
            <div class="submenu" id="submenu1">
               <li><a href="#">Sub Item 1</a></li>
               <li><a href="#">Sub Item 2</a></li>
            </div>
          <li><a href="#">Item 3</a></li>
          <li><a href="#">Item 4</a></li>
          <li><a href="#">Item 5<i class="fa fa-plus menutoggle" id="toggle5" aria-hidden="true"></i></a></li>
            <div class="submenu" id="submenu5">
             <li><a href="#">Sub Item 1</a></li>
             <li><a href="#">Sub Item 2</a></li>
            </div>
          <li><a href="#">Item 6</a></li>
          <li><a href="#">Item 7</a></li>
          <li><a href="#">Item 8</a></li>
          <li><a href="#">Item 9</a></li>
        </ul>
      </div>
    </div>
  </div>
</div>

jQuery:

jQuery(document).ready(function() {
   $(".submenu").slideUp("fast");
   $(".menutoggle").click(function(){
     var id = $(this).attr("id").replace("toggle","submenu");
     $("#" + id).slideToggle("fast");   
   });

  jQuery('#hamburger-nav-icon').click(function() {
    jQuery(this).toggleClass('open');
    jQuery('body').css('overflow','hidden');  
    if (jQuery('#hamburger-menu-cover').css('opacity') == '0') {
      jQuery('#hamburger-menu-cover').css('opacity', '1');
      jQuery('#hamburger-menu-cover').fadeIn(300).css('display', 'table'); 
        jQuery('body').css('overflow','hidden');
    } else {
        jQuery('#hamburger-menu-cover').css('opacity', '0');
        jQuery('#hamburger-menu-cover').fadeOut(300).css('display', 'none');
        jQuery('body').css('overflow','visible');
    }
  });
});

你需要这样的东西吗? https://jsfiddle.net/yz5p1czh/7/ 我在图标中添加了一个 open class 来触发 jquery 功能,该功能在子菜单 div.

中淡出

我已将您的 sub-items 移至 li > ul。您的旧 sub-items 现在也有一个 aria-hidden 属性。

添加的

CSS 控制 sub-items.

的可见性
.hamburger-menu-links ul[aria-hidden="true"] {
  display: none;
}

.hamburger-menu-links ul[aria-hidden="false"] {
  display: block;
}

已添加的 JS 代码为 ul 个子项切换 aria-hidden

$('.hamburger-menu-links li').click(function(e) {
  var ul = $(this).find('ul');

  var ariaHidden = ul.attr('aria-hidden');
  var value = (ariaHidden === 'true') || false;

  ul.attr('aria-hidden', !value);
});

这是工作示例。

jQuery(document).ready(function() {
  jQuery('#hamburger-nav-icon').click(function() {
    jQuery(this).toggleClass('open');
      jQuery('body').css('overflow','hidden');  
    if (jQuery('#hamburger-menu-cover').css('opacity') == '0') {
      jQuery('#hamburger-menu-cover').css('opacity', '1');
      jQuery('#hamburger-menu-cover').fadeIn(300).css('display', 'table'); 
        jQuery('body').css('overflow','hidden');
    } else {
      jQuery('#hamburger-menu-cover').css('opacity', '0');
      jQuery('#hamburger-menu-cover').fadeOut(300).css('display', 'none');
        jQuery('body').css('overflow','visible');
    }
  });
});


// new

$('.hamburger-menu-links li').click(function(e) {
  var ul = $(this).find('ul');
  
  var ariaHidden = ul.attr('aria-hidden');
  var value = (ariaHidden === 'true') || false;

  ul.attr('aria-hidden', !value);
});
/*--------------------------------------------------------------
## Hamburger Menu
--------------------------------------------------------------*/
#hamburger-menu-cover {
    z-index: 5;
    width: 100%;
    height: 100%;
  background: rgba(255, 255, 255, 0.9);
    position: fixed;
    font-size: 1.5em;
    text-align: center;
    right: 0px;
    top: 0px;
    opacity: 0;
    display: none;
    overflow-y: hidden;
   -webkit-overflow-scrolling: touch;
    overflow: hidden;
}
.hamburger-menu {
background:#fff;
   width: 31%;
    height: 100%;
  border-right: 1px solid #808080;
  position: fixed;
  top: 0px;
}
.hamburger-menu-container {
  margin-left: 73px;
  margin-top: 100px;
  padding-right: 32px;
  padding-bottom: 40px; 
  border-bottom: 1px solid #808080;
}
.hamburger-menu-links li  {
    list-style: none;
      font-family: 'freightlight';
  font-size: 36px;
  display: block;
  line-height: 57px;
    font-style: italic;
    text-transform: uppercase;
    text-decoration: none;
}
.hamburger-menu-links li a {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links li a:hover {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links li a:active {
    text-decoration: none;
    color:#000;
}
.hamburger-menu-links li a:visited {
    text-decoration: none;
    color:#000;
}

.hamburger-menu-links ul[aria-hidden="true"] {
  display: none;
}

.hamburger-menu-links ul[aria-hidden="false"] {
  display: block;
}

#hamburger-nav-icon {
    z-index: 20;
  width: 50px;
  height: 35px;
  position: relative;
  margin: 35px 30px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .5s ease-in-out;
  -moz-transition: .5s ease-in-out;
  -o-transition: .5s ease-in-out;
  transition: .5s ease-in-out;
  cursor: pointer;
}

#hamburger-nav-icon span {
  display: block;
  position: absolute;
  height: 5px;
  width: 40px;
  background: #000;
  opacity: 1;
  left: 0;
}

/* Icon 3 */

#hamburger-nav-icon span:nth-child(1) {
  top: 0px;
}

#hamburger-nav-icon span:nth-child(2),#hamburger-nav-icon span:nth-child(3) {
  top: 12px;
}

#hamburger-nav-icon span:nth-child(4) {
  top: 24px;
}

#hamburger-nav-icon.open span:nth-child(1) {
  top: 8px;
  width: 0%;
  left: 50%;
}

#hamburger-nav-icon.open span:nth-child(2) {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

#hamburger-nav-icon.open span:nth-child(3) {
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#hamburger-nav-icon.open span:nth-child(4) {
  top: 8px;
  width: 0%;
  left: 50%;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hamburger-nav-icon">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>
<div id="hamburger-menu-cover">
    <div class="hamburger-menu">
        <div class="hamburger-menu-container">
            <div class="hamburger-menu-links">
                <ul>
                    <li>
                        <a href="#">Item 1<i class="fa fa-plus"></i></a>
                        <ul aria-hidden="true">
                            <li>
                                <a href="#">Sub Item 1</a>
                            </li>
                            <li>
                                <a href="#">Sub Item 2</a>
                            </li> 
                        </ul>
                    </li>
                    <li>
                        <a href="#">Item 2<i class="fa fa-plus"></i></a>
                        <ul aria-hidden="false">
                            <li>
                                <a href="#">Sub Item 1</a>
                            </li>
                            <li>
                                <a href="#">Sub Item 2</a>
                            </li> 
                        </ul>
                    </li>
                    <li>
                        <a href="#">Item 3</a>
                    </li>
                    <li>
                        <a href="#">Item 4</a>
                    </li>
                    <li>
                        <a href="#">Item 5<i class="fa fa-plus"></i></a>
                        <ul aria-hidden="true">
                            <li>
                                <a href="#">Sub Item 1</a>
                            </li>
                            <li>
                                <a href="#">Sub Item 2</a>
                            </li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">Item 6</a>
                    </li>
                    <li>
                        <a href="#">Item 7</a>
                    </li>
                    <li>
                        <a href="#">Item 8</a>
                    </li>
                    <li>
                        <a href="#">Item 9</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>