【问题标题】:Make menu hamburger close when click on link and outside单击链接和外部时关闭菜单汉堡包
【发布时间】:2018-05-27 15:58:47
【问题描述】:

我为我的网站创建了一个简单的汉堡菜单,我只是无法在单击链接并转到我放入的锚点后关闭它,当我点击菜单时它也会关闭。

我的 HTML 是:

  $(document).ready(function() {
    $('.navbar-fostrap').click(function() {
      $('.nav-fostrap').toggleClass('visible');
      $('body').toggleClass('cover-bg');
    });
  });
nav {
  height: 60px;
  text-align: center;
  position: relative;
  width: width:100%;
  margin: auto;
  z-index: 100000;
}

.navbar-default {
  transition: 500ms ease;
  background: transparent;
}

.navbar-default.scrolled {
  background: rgba(28, 28, 28, 0.8);
}

a.title {
  float: left;
  font-size: 25px;
  color: white;
  font-family: quantum;
  height: 55px;
  padding-top: 2px;
}

.title {
  float: left;
  display: inline-block;
  padding: 0;
  margin-top: 5px;
  list-style: none;
  height: 55px;
}

.nav-fostrap ul {
  list-style-type: none;
  margin-top: 5px;
  padding: 0;
  display: block;
  float: right;
  width: auto;
}

.nav-fostrap ul li {
  display: inline-block;
  font-weight: bold;
  text-transform: uppercase;
  text-align: center;
  padding-left: 15px;
  padding-right: 15px;
  margin-top: 0;
  list-style: none;
  height: 60px;
}

a.menu {
  font-family: Montserrat, Arial;
  font-weight: 700;
  position: relative;
  display: inline-block;
  cursor: pointer;
  padding: 8px 0;
  margin-top: 14.5px;
  margin-bottom: 10.5px;
  color: #fff;
  text-decoration: none;
  height: 40px;
  top: -10px;
}

a:hover.menu {
  color: #fff;
  text-decoration: none;
}

.menu a:before,
a:after {
  content: '';
  position: absolute;
  width: 0%;
  height: 2px;
  bottom: -2px;
  background: #fff;
}

.menu a:before {
  left: 0;
}

.menu a:after {
  right: 0;
  background: #fff;
  transition: width 0.8s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.menu a:hover:before {
  background: #fff;
  width: 100%;
  transition: width 0.5s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.menu a:hover:after {
  background: transparent;
  width: 100%;
  transition: 0s;
}

.title-on-mobile {
  display: none;
}

@media only screen and (max-width:768px) {
  a.title {
    float: none;
    font-size: 25px;
    color: white;
    font-family: quantum;
    height: 55px;
    padding-top: 2px;
    width: 100%;
    text-align: center;
    position: fixed;
    left: -50px;
  }
  .nav-fostrap {
    background: #1c1c1c;
    width: 200px;
    height: 100%;
    display: block;
    position: fixed;
    left: -200px;
    top: 0px;
    -webkit-transition: left 0.25s ease;
    -moz-transition: left 0.25s ease;
    -ms-transition: left 0.25s ease;
    -o-transition: left 0.25s ease;
    transition: left 0.25s ease;
    margin: 0;
    border: 0;
    border-radius: 0;
    overflow-y: auto;
    overflow-x: hidden;
    height: 100%;
    z-index: 105;
  }
  .nav-fostrap.visible {
    left: 0px;
    -webkit-transition: left 0.25s ease;
    -moz-transition: left 0.25s ease;
    -ms-transition: left 0.25s ease;
    -o-transition: left 0.25s ease;
    transition: left 0.25s ease;
    z-index: 90;
  }
  .nav-bg-fostrap {
    display: inline-block;
    vertical-align: middle;
    width: 100%;
    height: 60px;
    margin: 0;
    position: absolute;
    top: 0px;
    left: 0px;
    background: transperant;
    padding: 12px 0 0 10px;
    -webkit-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -moz-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -ms-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -o-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    z-index: 100;
  }
  .navbar-fostrap {
    display: inline-block;
    vertical-align: middle;
    height: 50px;
    cursor: pointer;
    margin-top: 5px;
    position: absolute;
    top: 0;
    left: 0;
    padding: 12px;
    z-index: 102;
  }
  .navbar-fostrap span {
    height: 2px;
    background: #fff;
    margin: 5px;
    display: block;
    width: 20px;
  }
  .navbar-fostrap span:nth-child(2) {
    width: 20px;
  }
  .navbar-fostrap span:nth-child(3) {
    width: 20px;
  }
  .nav-fostrap ul {
    padding-top: 50px;
  }
  .nav-fostrap li {
    display: block;
  }
  .nav-fostrap li a {
    display: block;
    color: #505050;
    font-weight: 600;
  }
  .nav-fostrap li:first-child:hover a {
    border-radius: 0;
  }
  .cover-bg {
    background: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
}
<nav class="menu navbar navbar-default navbar-fixed-top">
  <div class="headercontainer">
    <a class="title" href="#seccao-inicio"><img src="img/logo.png" alt="logo" width="80" href="#" style="position: relative;margin-left: 12px; float:right; top:2.5px;position: relative;" /></a>

    <div class='nav-fostrap'>
      <ul class="menu">
        <li><a class="menu" href="#seccao-historia" style="color: white;">História</a></li>
        <li><a class="menu" href="#seccao-network" style="color: white;">Network</a></li>
        <li><a class="menu" href="#seccao-fundadores" style="color: white;">Fundadores</a></li>
        <li><a class="menu" href="contacto.html" style="color: white;">Contacto</a></li>
      </ul>
    </div>
    <div class='nav-bg-fostrap'>
      <div class='navbar-fostrap'> <span></span> <span></span> <span></span> </div>

    </div>
  </div>
</nav>

我正在为我的课程的专业能力项目开发此网站,因此欢迎任何帮助。如果有人可以帮助我,我非常感激,谢谢。

【问题讨论】:

  • 如果您在&lt;&gt; 编辑器中将所有这些 sn-ps 合并为一个,那么我们可以在页面中运行该代码并查看它的实际效果。通过将它们分成三个 sn-ps,css 和 js 不会与 html 关联,以便在单击 "Run code sn-p" 时协同工作
  • 检查我的答案,我更新它。

标签: javascript html css


【解决方案1】:

您可以使用这部分代码并添加您想要的任何其他逻辑。

$(document).click(function(e) {
    var target = e.target;
    if (
      !$(target).is(".navbar-fostrap") &&
      !$(target).parents(".navbar-fostrap").length &&
      !$(target).parents(".nav-fostrap").length
    ) {
      $(".nav-fostrap").removeClass("visible");
      $("body").removeClass("cover-bg");
    }
});

这里有完整的代码

$(document).ready(function() {
  $(".navbar-fostrap").click(function() {
    $(".nav-fostrap").toggleClass("visible");
    $("body").toggleClass("cover-bg");
  });
  $(document).click(function(e) {
    var target = e.target;
    if (!$(target).is(".navbar-fostrap") &&
      !$(target).parents(".navbar-fostrap").length &&
      !$(target).parents(".nav-fostrap").length
    ) {
      $(".nav-fostrap").removeClass("visible");
      $("body").removeClass("cover-bg");
    }
  });
});
body {
  background-color: red;
}

nav {
  height: 60px;
  text-align: center;
  position: relative;
  width: width:100%;
  margin: auto;
  z-index: 100000;
}

.navbar-default {
  transition: 500ms ease;
  background: transparent;
}

.navbar-default.scrolled {
  background: rgba(28, 28, 28, 0.8);
}

a.title {
  float: left;
  font-size: 25px;
  color: white;
  font-family: quantum;
  height: 55px;
  padding-top: 2px;
}

.title {
  float: left;
  display: inline-block;
  padding: 0;
  margin-top: 5px;
  list-style: none;
  height: 55px;
}

.nav-fostrap ul {
  list-style-type: none;
  margin-top: 5px;
  padding: 0;
  display: block;
  float: right;
  width: auto;
}

.nav-fostrap ul li {
  display: inline-block;
  font-weight: bold;
  text-transform: uppercase;
  text-align: center;
  padding-left: 15px;
  padding-right: 15px;
  margin-top: 0;
  list-style: none;
  height: 60px;
}

a.menu {
  font-family: Montserrat, Arial;
  font-weight: 700;
  position: relative;
  display: inline-block;
  cursor: pointer;
  padding: 8px 0;
  margin-top: 14.5px;
  margin-bottom: 10.5px;
  color: #fff;
  text-decoration: none;
  height: 40px;
  top: -10px;
}

a:hover.menu {
  color: #fff;
  text-decoration: none;
}

.menu a:before,
a:after {
  content: '';
  position: absolute;
  width: 0%;
  height: 2px;
  bottom: -2px;
  background: #fff;
}

.menu a:before {
  left: 0;
}

.menu a:after {
  right: 0;
  background: #fff;
  transition: width 0.8s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.menu a:hover:before {
  background: #fff;
  width: 100%;
  transition: width 0.5s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.menu a:hover:after {
  background: transparent;
  width: 100%;
  transition: 0s;
}

.title-on-mobile {
  display: none;
}

@media only screen and (max-width:768px) {
  a.title {
    float: none;
    font-size: 25px;
    color: white;
    font-family: quantum;
    height: 55px;
    padding-top: 2px;
    width: 100%;
    text-align: center;
    position: fixed;
    left: -50px;
  }
  .nav-fostrap {
    background: #1c1c1c;
    width: 200px;
    height: 100%;
    display: block;
    position: fixed;
    left: -200px;
    top: 0px;
    -webkit-transition: left 0.25s ease;
    -moz-transition: left 0.25s ease;
    -ms-transition: left 0.25s ease;
    -o-transition: left 0.25s ease;
    transition: left 0.25s ease;
    margin: 0;
    border: 0;
    border-radius: 0;
    overflow-y: auto;
    overflow-x: hidden;
    height: 100%;
    z-index: 105;
  }
  .nav-fostrap.visible {
    left: 0px;
    -webkit-transition: left 0.25s ease;
    -moz-transition: left 0.25s ease;
    -ms-transition: left 0.25s ease;
    -o-transition: left 0.25s ease;
    transition: left 0.25s ease;
    z-index: 90;
  }
  .nav-bg-fostrap {
    display: inline-block;
    vertical-align: middle;
    width: 100%;
    height: 60px;
    margin: 0;
    position: absolute;
    top: 0px;
    left: 0px;
    background: transperant;
    padding: 12px 0 0 10px;
    -webkit-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -moz-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -ms-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    -o-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
    z-index: 100;
  }
  .navbar-fostrap {
    display: inline-block;
    vertical-align: middle;
    height: 50px;
    cursor: pointer;
    margin-top: 5px;
    position: absolute;
    top: 0;
    left: 0;
    padding: 12px;
    z-index: 102;
  }
  .navbar-fostrap span {
    height: 2px;
    background: #fff;
    margin: 5px;
    display: block;
    width: 20px;
  }
  .navbar-fostrap span:nth-child(2) {
    width: 20px;
  }
  .navbar-fostrap span:nth-child(3) {
    width: 20px;
  }
  .nav-fostrap ul {
    padding-top: 50px;
  }
  .nav-fostrap li {
    display: block;
  }
  .nav-fostrap li a {
    display: block;
    color: #505050;
    font-weight: 600;
  }
  .nav-fostrap li:first-child:hover a {
    border-radius: 0;
  }
  .cover-bg {
    background: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<nav class="menu navbar navbar-default navbar-fixed-top">
  <div class="headercontainer">
    <a class="title" href="#seccao-inicio"><img src="img/logo.png" alt="logo" width="80" href="#" style="position: relative;margin-left: 12px; float:right; top:2.5px;position: relative;" /></a>

    <div class='nav-fostrap'>
      <ul class="menu">
        <li><a class="menu" href="#seccao-historia" style="color: white;">História</a></li>
        <li><a class="menu" href="#seccao-network" style="color: white;">Network</a></li>
        <li><a class="menu" href="#seccao-fundadores" style="color: white;">Fundadores</a></li>
        <li><a class="menu" href="contacto.html" style="color: white;">Contacto</a></li>
      </ul>
    </div>
    <div class='nav-bg-fostrap'>
      <div class='navbar-fostrap'> <span></span> <span></span> <span></span> </div>

    </div>
  </div>
</nav>

希望对你有帮助。

【讨论】:

  • Uau 非常感谢! :D 如果可能的话,再做一件事,您可以在打开菜单时将其放置为不返回站点顶部,并且当我单击菜单项(例如“Founders”)时,它会关闭并平滑滚动。您可以在此处查看示例,谢谢。 quantumn.atwebpages.com
  • 阅读我的答案here 我在那里回答。如果它没有帮助,问我@KR4K
  • 不幸的是,这并没有帮助我只是想要它,当我点击菜单时它没有回到顶部,当我点击链接时它会自行关闭,所以我不必点击关闭然后才滚动到我定义的部分。
  • 好的。给我时间。你能提供一个关于 plunker 或 codepen 的工作示例吗? @KR4K
  • CodePen 如果可能的话。谢谢你帮助我。
猜你喜欢
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
相关资源
最近更新 更多