javascript - Hamburgler.js 按钮在移动设备上不起作用

标签 javascript jquery html css

我已经实现了 hamburger.js。当我尝试在移动设备上显示我的网站菜单时,关闭按钮不起作用。

这是我的网站:http://johnm.io/project/hamburgler/

示例:

function togglescroll() {
  $('body').on('touchstart', function(e) {
    if ($('body').hasClass('noscroll')) {
      e.preventDefault();
    }
  });
}

$(document).ready(function() {
  togglescroll()
  $(".icon").click(function() {
    $(".mobilenav").fadeToggle(500);
    $(".top-menu").toggleClass("top-animate");
    $("body").toggleClass("noscroll");
    $(".mid-menu").toggleClass("mid-animate");
    $(".bottom-menu").toggleClass("bottom-animate");
  });
});

// PUSH ESC KEY TO EXIT

$(document).keydown(function(e) {
  if (e.keyCode == 27) {
    $(".mobilenav").fadeOut(500);
    $(".top-menu").removeClass("top-animate");
    $("body").removeClass("noscroll");
    $(".mid-menu").removeClass("mid-animate");
    $(".bottom-menu").removeClass("bottom-animate");
  }
});
* {
  font-family: 'helvetica nue', sans-serif;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  -webkit-tap-highlight-color: transparent;
  /* For some Androids */
}
.top-animate {
  background: #fff !important;
  top: 13px !important;
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}
.mid-animate {
  opacity: 0;
}
.bottom-animate {
  background: #fff !important;
  top: 13px !important;
  -webkit-transform: rotate(-225deg);
  transform: rotate(-225deg);
}
.top-menu {
  top: 5px;
  width: 25px;
  height: 2px;
  border-radius: 10px;
  background-color: #F9A530;
}
.mid-menu {
  top: 13px;
  width: 25px;
  height: 2px;
  border-radius: 10px;
  background-color: #F9A530;
}
.bottom-menu {
  top: 21px;
  width: 25px;
  height: 2px;
  border-radius: 10px;
  background-color: #F9A530;
}
.menui {
  background: orange;
  transition: 0.6s ease;
  transition-timing-function: cubic-bezier(.75, 0, .29, 1.01);
  margin-top: 10px;
  position: absolute;
}
.icon {
  z-index: 999;
  position: fixed;
  display: block;
  padding: 9px;
  height: 32px;
  width: 32px;
  margin: 0px;
  top: 0;
  left: 0;
}
.mobilenav {
  font-family: inherit;
  top: 0;
  left: 0;
  z-index: 999;
  display: none;
  position: fixed;
  width: 100%;
  height: 100%;
  background: orange;
}
.mobilenav li {
  list-style-type: none;
  text-align: center;
  padding: 10px;
}
.mobilenav li a {
  font-size: 150%;
  color: #fff;
  text-decoration: none;
  font-weight: 300;
  width: 100%;
}
.mobilenav li:first-child {
  margin-top: 60px;
}
<div class="mobilenav">
  <li><a href="#">Never</a>
  </li>
  <li><a href="#">Gonna</a>
  </li>
  <li><a href="#">Give</a>
  </li>
  <li><a href="#">You</a>
  </li>
  <li><a href="#">Up</a>
  </li>
</div>

<a href="javascript:void(0)" class="icon">
  <div class="hamburger">
    <div class="menui top-menu"></div>
    <div class="menui mid-menu"></div>
    <div class="menui bottom-menu"></div>
  </div>
</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

最佳答案

问题是 body 正在监听 touchstart 事件,如果他“拥有”类 noscroll - 这发生在菜单时是开放的 - 他做..好吧..什么都不做,而不是做切换。

因此,我们要做的是检查按钮是否触发了 touchstart 事件。如果是这样,我们进行切换。

// HAMBURGLERv2

function togglescroll() {
  $('body').on('touchstart', function(e) {
    // Just add the check: !$(e.target).hasClass('icon')
    if (!$(e.target).hasClass('icon') && $('body').hasClass('noscroll')) {
      e.preventDefault();
    }
  });
}

$(document).ready(function() {
  togglescroll()
  $(".icon").click(function() {
    $(".mobilenav").fadeToggle(500);
    $(".top-menu").toggleClass("top-animate");
    $("body").toggleClass("noscroll");
    $(".mid-menu").toggleClass("mid-animate");
    $(".bottom-menu").toggleClass("bottom-animate");
  });
});

// PUSH ESC KEY TO EXIT

$(document).keydown(function(e) {
  if (e.keyCode == 27) {
    $(".mobilenav").fadeOut(500);
    $(".top-menu").removeClass("top-animate");
    $("body").removeClass("noscroll");
    $(".mid-menu").removeClass("mid-animate");
    $(".bottom-menu").removeClass("bottom-animate");
  }
});
* {
  font-family: 'helvetica nue', sans-serif;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  -webkit-tap-highlight-color: transparent;
  /* For some Androids */
}

.top-animate {
  background: #fff !important;
  top: 13px !important;
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}

.mid-animate {
  opacity: 0;
}

.bottom-animate {
  background: #fff !important;
  top: 13px !important;
  -webkit-transform: rotate(-225deg);
  transform: rotate(-225deg);
}

.top-menu {
  top: 5px;
  width: 25px;
  height: 2px;
  border-radius: 10px;
  background-color: #F9A530;
}

.mid-menu {
  top: 13px;
  width: 25px;
  height: 2px;
  border-radius: 10px;
  background-color: #F9A530;
}

.bottom-menu {
  top: 21px;
  width: 25px;
  height: 2px;
  border-radius: 10px;
  background-color: #F9A530;
}

.menui {
  background: orange;
  transition: 0.6s ease;
  transition-timing-function: cubic-bezier(.75, 0, .29, 1.01);
  margin-top: 10px;
  position: absolute;
}

.icon {
  z-index: 999;
  position: fixed;
  display: block;
  padding: 9px;
  height: 32px;
  width: 32px;
  margin: 0px;
  top: 0;
  left: 0;
}

.mobilenav {
  font-family: inherit;
  top: 0;
  left: 0;
  z-index: 999;
  display: none;
  position: fixed;
  width: 100%;
  height: 100%;
  background: orange;
}

.mobilenav li {
  list-style-type: none;
  text-align: center;
  padding: 10px;
}

.mobilenav li a {
  font-size: 150%;
  color: #fff;
  text-decoration: none;
  font-weight: 300;
  width: 100%;
}

.mobilenav li:first-child {
  margin-top: 60px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mobilenav">
  <li><a href="#">Never</a></li>
  <li><a href="#">Gonna</a></li>
  <li><a href="#">Give</a></li>
  <li><a href="#">You</a></li>
  <li><a href="#">Up</a></li>
</div>

<a href="javascript:void(0)" class="icon">
  <div class="hamburger">
    <div class="menui top-menu"></div>
    <div class="menui mid-menu"></div>
    <div class="menui bottom-menu"></div>
  </div>
</a>

我也用这里的代码修复了笔:http://codepen.io/anon/pen/gayZYG

关于javascript - Hamburgler.js 按钮在移动设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33856925/

相关文章:

javascript - 查找元素是否包含在 jquery 选择器中

javascript - TypeScript 函数式编程模式是否能实现舒适的对象构造?

javascript - 如何计算乘客总数

html - 边框父 div 和子 div 之间的空间

javascript - 为什么我的 javascript 函数不执行?错误 : Cannot read property 'change' of undefined

javascript - 从谷歌浏览器的地址栏获取文本(不是 url)

javascript - jQuery Listview 更改列表项颜色

jQuery : How to know if input type is not radio

html - 开发 WebInterface 的建议(使用 Tomcat)?

javascript - 如何获得与 Youtube 相同的按钮?