【问题标题】:navbar links still clickable but invisible when menu is "closed"当菜单“关闭”时,导航栏链接仍然可点击但不可见
【发布时间】:2020-07-14 12:48:21
【问题描述】:

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

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

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

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>

Codepen:https://codepen.io/Ribeye/pen/BajOoeb

【问题讨论】:

    标签: html jquery css navbar


    【解决方案1】:

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

    nav {
        overflow: hidden;
    }
    

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

    【讨论】:

      【解决方案2】:

      您应该将pointer-events: none 添加到.navpointer-events: auto.open-menu ~ nav

      自动

      元素的行为与pointer-events 属性相同 未指定。在 SVG 内容中,这个值和值 visiblePainted 效果一样。

      元素永远不是指针事件的target;然而, 指针事件可能以它的后代元素为目标,如果这些后代 将pointer-events 设置为其他值。在这些情况下, 指针事件将触发此父元素上的事件侦听器 在活动期间适合他们往返后代的途中 捕获/bubble 阶段。

      Ref

      var $header_top = $('.header-top');
      var $nav = $('nav');
       
      $header_top.find('a').on('click', function() {
        $(this).parent().toggleClass('open-menu');
      });
      /* added */
      nav {
        pointer-events: none;
      }
      
      .open-menu~nav {
        pointer-events: auto;
      }
      /*****/
      
      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;
          }
        }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      <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>
            <l>
              <a href="https://www.google.com ">Data Protection</a>
            </l>
            <l>
              <a href="https://www.google.com ">Data Protection</a>
            </l>
      
          </ul>
        </nav>
      </header>

      【讨论】:

      • 感谢您的回答!如果我可以问,指针事件有什么作用?
      【解决方案3】:

      您只需在nav 元素上设置overflow: hidden 属性

      【讨论】:

        猜你喜欢
        • 2019-06-22
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 2019-04-18
        • 1970-01-01
        • 1970-01-01
        • 2013-02-21
        相关资源
        最近更新 更多