移动导航不会使用锚链接在单页网站上崩溃

时间:2017-12-31 16:16:41

标签: javascript jquery mobile navigation single-page-application

我不确定如果从菜单中点击锚链接,当前滑出到页面上的移动导航将会崩溃。目前,菜单只是保持打开状态,遮盖下面的内容,直到用户点击" X"按钮。

这是我的HTML:

<div class="mobile-nav-button">
   <div class="mobile-nav-button__line"></div>
   <div class="mobile-nav-button__line"></div>
   <div class="mobile-nav-button__line"></div>
</div>

<nav class="mobile-menu">
  <ul>
    <li><img src="../img/kct_logo.png"></li>
    <li><a href="#about">ABOUT</a></li>
    <li><a href="#services">SERVICES</a></li>
    <li><a href="#history">HISTORY</a></li>
    <li><a href="#pricing">PRICING</a></li>
    <li><a href="#testimonials">TESTIMONIALS</a></li>
    <li><a href="#contact">CONTACT</a></li>
  </ul>
</nav>

这是相应的CSS:

.mobile-nav-button {
  width: 35px;
  position: absolute;
  margin: 2rem;
  right: 0;
  top: 0;
  z-index: 9999;
  cursor: pointer;
  width: 35px;
  height: 30px;
  visibility: hidden;
}

.mobile-nav-button .mobile-nav-button__line {
  width: 100%;
  height: 3px;
  background: white;
  position: relative;
  transition: 0.3s ease;
}

.mobile-nav-button .mobile-nav-button__line:nth-of-type(2) {
margin: 0.5rem 0;
}

.mobile-nav-button .mobile-nav-button__line--1 {
  transform: rotate(45deg);
  top: 13px;
  position: absolute;
}

.mobile-nav-button .mobile-nav-button__line--2 {
  display: none;
}

.mobile-nav-button .mobile-nav-button__line--3 {
  transform: rotate(135deg);
  top: 13px;
  position: absolute;
}

.mobile-menu {
  display: block;
  max-width: 500px;
  width: 100%;
  right: -100%;
  height: 100vh;
  background: #091525;
  position: absolute;
  z-index: 9998;
  transition: 0.2s ease;
  top: 0;
  opacity: 1;
}

.mobile-menu ul {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  padding: 0;
  margin-right: 0;
}

.mobile-menu ul li {
  list-style: none;
}

.mobile-menu ul li a {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  display: block;
  text-align: center;
  text-decoration: none;
  color: white;
  font-family: 'Source Sans Pro', sans-serif;
  font-size: 20px;
  font-weight: 300;
  line-height: 50px;
  letter-spacing: 2px;
  overflow: hidden;
  position: relative;
}

.mobile-menu ul li a:after {
  content: '';
  background: #835a43; 
  width: 100%;
  height: 100%;
  position: absolute;
  right: -100%;
  top: 0;
  z-index: -1;
  transition: 0.4s ease;
}

.mobile-menu ul li a:hover {
  color: #fff;
}

.mobile-menu ul li a:hover:after {
  right: 0;
}

.mobile-menu img {
  position: absolute;
  width: 180px;
  display: block;
  left: 50%; 
  top: -4rem;
  transform: translatex(-50%);
  padding: 0;
  text-align: center;
}

.mobile-menu--open {
 right: 0;
 opacity: 1;
}

最后是JS:

$(document).ready(function () { 
//Menu button on click event
$('.mobile-nav-button').on('click', function() {
// Toggles a class on the menu button to transform the burger menu into 
a cross
$( ".mobile-nav-button .mobile-nav-button__line:nth-of-type(1)" 
).toggleClass( "mobile-nav-button__line--1");
$( ".mobile-nav-button .mobile-nav-button__line:nth-of-type(2)" 
).toggleClass( "mobile-nav-button__line--2");  
$( ".mobile-nav-button .mobile-nav-button__line:nth-of-type(3)" 
).toggleClass( "mobile-nav-button__line--3");  

// Toggles a class that slides the menu into view on the screen
$('.mobile-menu').toggleClass('mobile-menu--open');
return false;
}); 
});

只是想知道我是否需要编写一些专门针对菜单中任何锚链接的JS,然后一旦点击其中任何一个,删除&#34;移动菜单 - 打开&#34; class为了折叠滑动菜单?

0 个答案:

没有答案