javascript - 响应式菜单不起作用(菜单正在关闭)

标签 javascript html css

我在 html/css/javascript 中有这个菜单,当您单击菜单然后尝试转到菜单中的页面之一时,没有任何重定向并且菜单关闭。即使在 html 中我向这些页面添加了带有 href 的标签

HTML:

<div id="pattern" class="pattern menu-link" style="max-width:574px;  min-width:300px;">
  <a href="#"><span style="font-size:27px; font-weight: bold; padding-top:    20px;" id="menustylish">&#9776; MENU</span></a>
  <nav id="menu" role="navigation">
    <ul>
      <li><a style="color:white;" href="index.html">Homepage</a></li>
      <li><a style="color:white;" href="login.html">Log in</a></li>
      <li><a style="color:white;" href="signup.html">Sign up</a></li>
    </ul>
  </nav>
</div>

CSS:

a {
  color: #DADDDE;
  text-decoration: none;
}
a:hover,
a:focus {
  color: #DADDDE;
}
p {
  margin: 1em;
  text-align: center;
}
body p a {
  color: #DADDDE;
}
.pattern {
  background: #333;
  border-bottom: 1px solid #808080;
  margin-bottom: 1em;
  overflow: hidden;
}
a.menu-link {
  float: left;
  display: block;
  padding: 1em;
}
nav[role=navigation] {
  clear: both;
  -webkit-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -ms-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}
.js nav[role=navigation] {
  overflow: hidden;
  max-height: 0;
}
nav[role=navigation].active {
  max-height: 15em;
}
nav[role=navigation] ul {
  margin: 0;
  padding: 0;
  border-top: 1px solid #808080;
}
nav[role=navigation] li a {
  display: block;
  padding: 0.8em;
  border-bottom: 1px solid #808080;
}
@media screen and (min-width: 48.25em) {
  a.menu-link {
    display: none;
  }
  .js nav[role=navigation] {
    max-height: none;
  }
  nav[role=navigation] ul {
    margin: 0 0 0 -0.25em;
    border: 0;
  }
  nav[role=navigation] li {
    display: inline-block;
    margin: 0 0.25em;
  }
  nav[role=navigation] li a {
    border: 0;
  }
}

JavaScript:

$(document).ready(function() {
  $('body').addClass('js');
  var $menu = $('#menu'),
    $menulink = $('.menu-link');

  $menulink.click(function() {
    $menulink.toggleClass('active');
    $menu.toggleClass('active');
    return false;
  });
});

最佳答案

这样就可以了,这里您从点击函数返回 false,因此它将阻止从链接重定向的默认行为。所以不要返回 false,它会起作用

$(document).ready(function() {
  $('body').addClass('js');
  var $menu = $('#menu'),
    $menulink = $('.menu-link');

  $menulink.click(function() {
    $menulink.toggleClass('active');
    $menu.toggleClass('active');
    //return false; // Don't return anything
  });
});

关于javascript - 响应式菜单不起作用(菜单正在关闭),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37762076/

相关文章:

javascript - 一张 Angular ng-map 中的标记重新出现在错误的 View 中

php - Kohana - 发布带有 ".exec"正则表达式的 javascript 代码返回内部错误

javascript - Vuejs : Accessing Specific URL Segment

html - 如何使用 Hover 更改 &lt;iframe&gt; 元素的不透明度?

javascript - 如何使用 setTimeout 添加和删除 CSS 类 n 次?

javascript - jquery 和 bootstrap 之间的冲突

javascript - 停止表单发送并执行函数

php - 使用来自各种 MySQL 表的信息在 HTML 上创建表

javascript - 革命性的 slider 快速淡入淡出

javascript - 表单提交连接到 js 事件不起作用