如何在移动汉堡包菜单中心并防止单击菜单项时我的菜单消失?

时间:2018-01-22 10:01:04

标签: jquery html css

我希望我的移动汉堡菜单和子菜单项都居中,当我点击其中一个包含子菜单项的菜单项时,它就不会消失。我在CSS中尝试了不同的东西,但我似乎无法自己解决这个问题。

这是我的网站(使用移动视图):http://sstromberg.saldev.nl/index.html

有人可以帮助我吗?

HTML标记:

<body>
 <nav>
    <div id="hamburger-menu">
        <div class="bar1"></div>
        <div class="bar2"></div>
        <div class="bar3"></div>
     <ul id="menu">
        <li><a href="index.html">Home</a></li>
        <li>
           <a>CV</a>
           <ul class="submenu">
              <li><a href="pgegevens.html">Persoonlijke Gegevens</a></li>
              <li><a href="werkervaring.html">Werkervaring</a></li>
              <li><a href="opleiding.html">Opleiding</a></li>
           </ul>
        </li>
        <li>
           <a>Portfolio</a>
           <ul class="submenu">
              <li><a href="dezewebsite.html">Deze Website</a></li>
              <li><a href="klassenlijst.html">Klassenlijst</a></li>
              <li><a href="todolist.html">TODO-list</a></li>
           </ul>
        </li>
        <li>
           <a>Overig</a>
           <ul class="submenu">
              <li><a href="socialmedia.html">Social Media</a></li>
              <li><a href="gmaps.html">Google Maps</a></li>
           </ul>
        </li>
     </ul>
     </div>
  </nav>

CSS代码:

@media only screen and (min-device-width : 320px) and (max-device-width : 480px)  {
    .bar1, .bar2, .bar3 {
    width: 35px;
    height: 5px;
    background-color: #333;
    margin: 6px 0;

}
ul#menu > li {
    width: 100%;
    display: block;
}
ul#menu > li > ul.submenu {
    display: none;
}

ul#menu > li:hover > ul.submenu {
    display: block;    
    list-style: none;
    margin: 0;
    padding: 0;
    background-color: #f3f3f3; 
}

ul#menu a:hover {
    transform: scale(1.1);
}

ul#menu {
    list-style: none;
    margin: 0;
    padding: 0;
    text-align: center;
    font-family: sans-serif;
    background-color: #f3f3f3;
    z-index: 50;
}

ul#menu a {
    display: block;
    padding: 1em; /* met 1em word de ruimte tussen de list items 2x zo groot */
    font-family: sans-serif;
    color: #FF4136;
    text-decoration-line: none;
    font-weight: bold;
}

.submenu {
    z-index: 100;
}
}

jQuery代码:

$(document).ready(function() {
    $('#hamburger-menu').on('click', function () {
        $("#menu").slideToggle();
    });
});

1 个答案:

答案 0 :(得分:1)

首先将菜单栏包装成div,然后应用margin:auto宽度。它将使图标居中。

并且还应用icon上的点击事件而不是整个div

$(document).ready(function() {
  $('#hamburger-menu .icon').on('click', function() {
    $("#menu").slideToggle();
  });
});
@media only screen and (min-width: 320px) and (max-width: 700px) {
  .bar1,
  .bar2,
  .bar3 {
    width: 35px;
    height: 5px;
    background-color: #333;
    margin: 6px 0;
  }
  .icon {
    width: 35px;
    margin: auto;
  }
  ul#menu>li {
    width: 100%;
    display: block;
  }
  ul#menu>li>ul.submenu {
    display: none;
  }
  ul#menu>li:hover>ul.submenu {
    display: block;
    list-style: none;
    margin: 0;
    padding: 0;
    background-color: #f3f3f3;
  }
  ul#menu a:hover {
    transform: scale(1.1);
  }
  ul#menu {
    list-style: none;
    margin: 0;
    padding: 0;
    text-align: center;
    font-family: sans-serif;
    background-color: #f3f3f3;
    z-index: 50;
  }
  ul#menu a {
    display: block;
    padding: 1em;
    /* met 1em word de ruimte tussen de list items 2x zo groot */
    font-family: sans-serif;
    color: #FF4136;
    text-decoration-line: none;
    font-weight: bold;
  }
  .submenu {
    z-index: 100;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div id="hamburger-menu">
    <div class="icon">
      <div class="bar1"></div>
      <div class="bar2"></div>
      <div class="bar3"></div>
    </div>
    <ul id="menu">
      <li><a href="index.html">Home</a></li>
      <li>
        <a>CV</a>
        <ul class="submenu">
          <li><a href="pgegevens.html">Persoonlijke Gegevens</a></li>
          <li><a href="werkervaring.html">Werkervaring</a></li>
          <li><a href="opleiding.html">Opleiding</a></li>
        </ul>
      </li>
      <li>
        <a>Portfolio</a>
        <ul class="submenu">
          <li><a href="dezewebsite.html">Deze Website</a></li>
          <li><a href="klassenlijst.html">Klassenlijst</a></li>
          <li><a href="todolist.html">TODO-list</a></li>
        </ul>
      </li>
      <li>
        <a>Overig</a>
        <ul class="submenu">
          <li><a href="socialmedia.html">Social Media</a></li>
          <li><a href="gmaps.html">Google Maps</a></li>
        </ul>
      </li>
    </ul>
  </div>
</nav>