.addClass #mainNav:可见

时间:2017-04-17 16:52:20

标签: javascript jquery visible

所需 单击菜单按钮并导航(#mainNav)时,请检查是否可见。如果是这样,.addClass .is-打开.nav-item。当导航(#mainNav)关闭时,.removeClass .is-从.nav-item打开。

问题: 当导航可见时(幻灯片),类未被添加到.nav-item。

HTML

<header>
  <!-- Logo and Burger -->
  <div class="brand-wrap">
    <div class="trigger-wrapper">
      <button id="burger">
        <div class="nav-trigger-line"></div>
        <div class="nav-trigger-line"></div>
        <div class="nav-trigger-line"></div>
      </button>
    </div>
  </div>
  <!-- End of Logo and Burger -->

  <!-- Navigation -->
  <nav id="mainNav" class="navbar main-nav">
    <div class="nav-container">
      <ul class="nav flex-column">
        <li class="nav-item">
          <a href="company.html" class="nav-link"><span class="nav-number">01.</span><br>Our Company</a>
        </li>
        <li class="nav-item">
          <a href="people.html" class="nav-link"><span class="nav-number">02.</span><br>Our People</a>
        </li>
        <li class="nav-item">
          <a href="services.html" class="nav-link">
            <span class="nav-number">03.</span>
            <br>Services</a>
        </li>
        <li class="nav-item">
          <a href="careers.html" class="nav-link"><span class="nav-number">04.</span><br>Careers</a>
        </li>
        <li class="nav-item">
          <a href="contact.html" class="nav-link"><span class="nav-number">05.</span><br>Contact Us</a>
        </li>
      </ul>
    </div>
  </nav>
  <!-- End of Navigation -->
</header>

CSS

 .brand-wrap {
      position: fixed;
      top: 20px;
      right: 20px;
      z-index: 999;
    }

    #burger {
      float: right;
      margin: 0;
    }

    .trigger-wrapper {
      position: absolute;
      right: 5px;
    }

    .main-nav a {
      text-decoration: none;
    }

    .main-nav {
      display: none;
      position: fixed;
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
      background-color: rgba(35, 31, 32, 1);
    }

    .nav-container {
      margin-top: 80px;
    }

    .nav-link {
      color: #fff;
      font-size: 1.5em;
      font-weight: 800;
      padding: .25em 1em;
    }

    .nav-link:hover {
      color: #82bc00;
    }

    .nav-number {
      font-size: .5em;
      font-weight: 600;
    }

    .nav-trigger-line {
      height: 3px;
      width: 30px;
      background-color: #fff;
      margin: 6px auto;
    }    

JS

// Menu click function
$('#burger').click(function() {
  $('#mainNav').toggle();
});

// Check if Nav is visible
if ($('#mainNav').is(':visible')) {
  $(".nav-container .nav .nav-item").addClass("is-open");
} else {
  $(".nav-container .nav .nav-item").removeClass("is-open");
}

jsfiddle:https://jsfiddle.net/WeebleWobb/auszo29m/2/

1 个答案:

答案 0 :(得分:2)

您需要在事件处理程序中发送

// Menu click function
$('#burger').click(function() {
  $('#mainNav').toggle(function () {
    // Check if Nav is visible
    if ($('#mainNav').is(':visible')) {
      $(".nav-container .nav .nav-item").addClass("is-open");
    } else {
      $(".nav-container .nav .nav-item").removeClass("is-open");
    }
  });
});

你能做的最好的事情是,你应该把它放在上面的回调函数中。