html - 当菜单为 "closed"时,导航栏链接仍然可点击但不可见

标签 html jquery css navbar

我正在使用导航栏模板,但遇到一个问题,即导航栏链接仍然存在于页面上,并且即使导航栏“关闭”时也可单击。

我正在考虑也许用脚本更改类,修改现有的类,但实际上我不知道该怎么做。

关闭导航栏时隐藏链接或暂时删除链接的最佳方法是什么?

var $header_top = $('.header-top');
var $nav = $('nav');

$header_top.find('a').on('click', function() {
  $(this).parent().toggleClass('open-menu');
});
  a {
  text-decoration: none;
  color: white;
}

ul,
li {
  list-style-type: none;
}

.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
}

.clearfix:after {
  clear: both;
}

.l-left {
  float: left;
}

.l-right {
  float: right;
}

.end {
  margin-top: 30px;
  font-size: 3em;
  font-weight: bold;
  opacity: 0;
  -webkit-transform: translateY(300px);
  transform: translateY(300px);
  transition: opacity, -webkit-transform 1s;
  transition: opacity, transform 1s;
  transition: opacity, transform 1s, -webkit-transform 1s;
  transition-delay: 1s;
}

.header-top {
  background: white;
  height: 70px;
  padding: 0 10px;
  position: fixed;
  top: 0;
  width: 100%;
  min-width: 300px;
  z-index: 12;
  box-sizing: border-box;
}

h1 {
  line-height: 70px;
  height: 70px;
}

h1 a {
  color: red;
  padding: 0 10px;
  font-family: "arial black";
  font-size: 35px;
}

.first-letter {
  color: red;
  padding: 0px;
  font-family: "arial black";
  font-size: 45px;
}

.toggle-menu {
  width: 50px;
  height: 50px;
  display: inline-block;
  position: relative;
  top: 10px;
}

.toggle-menu i {
  position: absolute;
  display: block;
  height: 2px;
  background: red;
  width: 30px;
  left: 10px;
  transition: all .3s;
}

.toggle-menu i:nth-child(1) {
  top: 16px;
}

.toggle-menu i:nth-child(2) {
  top: 24px;
}

.toggle-menu i:nth-child(3) {
  top: 32px;
}

.open-menu i:nth-child(1) {
  top: 25px;
  -webkit-transform: rotateZ(45deg);
  transform: rotateZ(45deg);
}

.open-menu i:nth-child(2) {
  background: transparent;
}

.open-menu i:nth-child(3) {
  top: 25px;
  -webkit-transform: rotateZ(-45deg);
  transform: rotateZ(-45deg);
}

nav {
  height: 0;
  opacity: 0;
  box-sizing: border-box;
  background: rgba(0, 47, 77, .25);
  position: fixed;
  top: 70px;
  width: 100%;
  transition: all 1s;
}

.open-menu~nav {
  opacity: 1;
  padding: 80px 0;
  z-index: 15;
  height: calc(90vh - 70px);
}

nav ul {
  padding: 0 10px;
  display: flex;
}

nav li {
  flex: 1;
}

nav li a {
  font-size: 2em;
  display: block;
  padding: 30px;
  text-align: center;
  transition: background .3s;
}

nav li a {
  background: #ff4b4b;
  margin-left: 20px;
}

nav li a:hover {
  background: #ADD8E6;
}


/*These 3 sections add the drop dwon menus in the headers*/

ul li ul.services-dropdown {
  display: none;
  z-index: 999;
  width: 100%;
}

ul li:hover ul.services-dropdown {
  display: inline-block;
  /* Display the dropdown */
}

ul li ul.services-dropdown li {
  display: block;
}

section {
  text-align: center;
}

h2 {
  font-size: 13px;
}

h2 a {
  padding: 8 8 8 8px;
  float: left;
  color: white;
  background-color: red;
  font-family: 'Open Sans';
  font-weight: bold;
}

h3 {
  font-weight: bold;
  font-size: 60px;
  color: white;
}

#fp-nav ul li a span,
.fp-slidesNav ul li a span {
  background: white;
  width: 8px;
  height: 8px;
  margin: -4px 0 0 -4px;
}

#fp-nav ul li a.active span,
.fp-slidesNav ul li a.active span,
#fp-nav ul li:hover a.active span,
.fp-slidesNav ul li:hover a.active span {
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;
  background: transparent;
  box-sizing: border-box;
  border: 2px solid #212121;
}

@media (max-width: 700px) {
  .edit-name {
    display: none;
  }
}


/*Removes the tel and email when window is narrow */

@media (max-width: 1230px) {
  .narrow-hide {
    display: none;
  }
}


/*Edits the nav bar when window is narrow */

@media screen and (max-width: 767px) {
  nav ul {
    flex-direction: column;
  }
  nav li {
    margin-top: 1px;
  }
  nav li a {
    font-size: 1.5em;
  }
  .scroll-icon {
    display: none;
  }
  @media screen and (max-width: 400px) {
    html {
      font-size: 50%;
    }
    .open-menu~nav {
      padding: 20px 0;
    }
    nav li a {
      padding: 3px;
    }
  }
<header>
  <div class="header-top clearfix">
    <a class="l-right toggle-menu" href="#">
      <i></i>
      <i></i>
      <i></i>
    </a>
  </div>
  <nav class="hide">
    <ul id="menu">
      <li>
        <a href="https://www.google.com ">Home</a>
          </li>
          <li>
            <a href="https://www.google.com "> Services</a>
                 <ul class="services-dropdown ">
                    <li><a href="https://www.google.com ">whats</a>   </li>
                    <li><a href="https://www.google.com ">Stuff</a></li>
                    <li><a href="https://www.google.com ">Things</a></li>
                </ul>
          </li>
          <li>
            <a href="https://www.google.com ">News & Events</a>
          </li>
          <li>
            <a href="https://www.google.com ">Contact Us</a>
          </li>
          <li>
            <a href="https://www.google.com ">Data Protection</a>
          </li>
          <li>
            <a href="https://www.google.com ">Data Protection</a>
          </li>                               
        </ul>
      </nav>
    </header>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js "></script>

代码笔:https://codepen.io/Ribeye/pen/BajOoeb

最佳答案

只需将 overflow:hidden 添加到 nav 即可修复;

nav {
    overflow: hidden;
}

您已经设置了 height: 0opacity: 0,但内容仍然溢出,并且其不可见的唯一原因是 opacity: 0 属性,这只是使其透明,因此仍然可点击。

关于html - 当菜单为 "closed"时,导航栏链接仍然可点击但不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62895581/

相关文章:

javascript - 将鼠标悬停在日期选择器中时显示箭头

css - 是否可以使用网格布局来制作带有卡住/固定标题的表格

html css 下拉菜单帮助使它出现和消失

javascript - 为什么 Markdown 用 id 渲染 <h1> ?

javascript - 上传文件 jQuery ajax MVC

html - CSS 百分比在 IE 中完全失败

jquery - 如何使DIV在不可点击的区域可点击?

html - 固定位置 float 按钮到页面右侧,无论父 div 上的滚动条如何

html - 不同屏幕分辨率下嵌套div结构中div的高度

jquery - Android 上的输入类型日期验证