在汉堡包点击菜单上添加课程

时间:2016-07-24 19:46:54

标签: javascript jquery menu overlay

我一直在前后退步。

我正在使用https://jonsuh.com/hamburgers/移动汉堡包动画。

问题是,我点击汉堡包,菜单覆盖图打开。但是,如果单击叠加层,它将消失,而不会重置汉堡包。 X仍然存在。

第二个问题,也是我一直试图用jQuery修复的问题。这是我第一次使用它,所以请耐心等待。

我向下滚动菜单淡入淡出但是,我一直在尝试将“删除”类注入“菜单”类。这样我就可以将CSS设置为none。目前它位于叠加层的顶部并且看起来不太好。

$(document).ready(function() {

  $(".hamburger").click(function() {
    $(".overlay").fadeToggle(200);
  });
});

$('.overlay').on('click', function() {
  $(".overlay").fadeToggle(200);

  open = false;
});

var $hamburger = $(".hamburger");
$hamburger.on("click", function(e) {
  $hamburger.toggleClass("is-active"), $(".overlay").fadeToggle(200);
  // Do something else, like open / close menu
});
var $hamburger = $(".hamburger");
$hamburger.on("click", function(e) {
  $hamburger.removeclass("is-active");

});

$(".hamburger").click(function() {
  $('#menu').addClass('remove');
});

$(".hamburger").click(function() {
  $('#menu').removeClass('');
});

(function($) {
  $(document).ready(function() {
    $(window).scroll(function() {
      if ($(this).scrollTop() > 100) {
        $('#menu').fadeIn(500);
      } else {
        $('#menu').fadeOut(500);
      }
    });
  });
})(jQuery);
.hamburger {
  font: inherit;
  display: inline-block;
  overflow: visible;
  margin: 0;
  padding: 15px;
  cursor: pointer;
  transition-timing-function: linear;
  transition-duration: .15s;
  transition-property: opacity, -webkit-filter;
  transition-property: opacity, filter;
  transition-property: opacity, filter, -webkit-filter;
  text-transform: none;
  color: inherit;
  border: 0;
  background-color: transparent;
  position: absolute;
  z-index: 99999;
}
.hamburger:hover {
  opacity: .7
}
.hamburger-box {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 24px
}
.hamburger-inner {
  top: 50%;
  display: block;
  margin-top: -2px
}
.hamburger-inner,
.hamburger-inner:after,
.hamburger-inner:before {
  position: absolute;
  width: 40px;
  height: 4px;
  transition-timing-function: ease;
  transition-duration: .15s;
  transition-property: -webkit-transform;
  transition-property: transform;
  transition-property: transform, -webkit-transform;
  border-radius: 4px;
  background-color: #000
}
.hamburger-inner:after,
.hamburger-inner:before {
  display: block;
  content: ''
}
.hamburger-inner:before {
  top: -10px
}
.hamburger-inner:after {
  bottom: -10px
}
.menu-overlay {
  position: fixed;
  background-color: transparent;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  transition: all 300ms ease-in-out
}
#menu {
  position: fixed;
  top: 0px;
  width: 100%;
  height: 65px;
  z-index: 4;
  display: none;
  background: red;
}
.menu {
  margin: 0
}
nav {
  text-align: center;
  display: inline-block;
  position: relative;
  height: auto;
  overflow: hidden;
  background: none;
  float: right
}
nav ul {
  text-align: center;
  margin: 0;
  padding: 0;
  text-decoration: none;
  list-style: none;
  float: right;
  margin: 0 .3rem 0
}
nav ul li {
  display: inline-block;
  letter-spacing: 3px;
  font-size: .5rem !important;
  font-family: "Montserrat", sans-serif;
  transition-property: all .2s linear 0s;
  -moz-transition: all .2s linear 0s;
  -webkit-transition: all .2s linear 0s;
  -o-transition: all .2s linear 0s;
  border-top: 3px solid transparent
}
nav ul li:hover {
  border-top: 3px solid #FFC3AF
}
nav ul li a {
  color: #2d2d2d;
  font-size: 16px;
  text-transform: uppercase;
  font-weight: 500;
  font-size: 13px;
  display: inline-block;
  display: inline-block;
  padding: 1.2rem 1.5rem;
  text-decoration: none;
  border-bottom: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menu">
  <div class="hamburger hamburger--elastic" tabindex="0" aria-label="Menu" role="button" aria-controls="navigation">
    <div class="hamburger-box">
      <div class="hamburger-inner"></div>
    </div>
  </div>
  <nav class="nav-scroll">
    <ul>
      <li><a href="#home">Home</a>
      </li>
      <li><a href="#about">About</a>
      </li>
      <li><a href="#testimonials">Testimonials</a>
      </li>
      <li><a href="#portfolio">Portfolio</a>
      </li>
      <li><a href="#services">Services</a>
      </li>
      <li><a href="#contact">Contact</a>
      </li>
    </ul>
  </nav>
</div>
<div class="overlay" id="navigation">
  <div class="wrap">
    <ul class="wrap-nav">
      <li><a href="#home">Home</a>
      </li>
      <li><a href="#about">About</a>
      </li>
      <li><a href="#testimonials">Testimonials</a>
      </li>
      <li><a href="#portfolio">Portfolio</a>
      </li>
      <li><a href="#services">Services</a>
      </li>
      <li><a href="#contact">Contact</a>
      </li>
    </ul>
  </div>
  <!-- end overlay - wrap -->
</div>
<!-- end overlay -->

1 个答案:

答案 0 :(得分:0)

我检查了一下,我得到了解决方案。你有这个代码,只需做一些改动:

$('.overlay').on('click', function() {
  $(".overlay").fadeToggle(200);   
  // Add this:
  $(".hamburger").toggleClass("is-active");
  open = false;
});

您只需要为is-active切换此类:.hamburger。另一件事是,您遇到的错误是:

$hamburger.removeclass("is-active");
//---------------^ Should be C not c.

以上是拼写错误。将其更改为:

$hamburger.removeClass("is-active");