【问题标题】:Hide Elements and Disable Scroll when Mobile Navbar is open移动导航栏打开时隐藏元素并禁用滚动
【发布时间】:2021-11-14 09:21:41
【问题描述】:

所以,我的移动导航栏有问题。这是一个简单的响应式导航栏。但是当我在手机上打开它时,会显示主体上的元素。这个gif显示了一切????

我将这段代码用于导航栏:

HTML

 <div class="container">
<div class="logo">
  <a href="#"><img src="logo.png" alt="logo"></a>
</div>
<div class="navbar">

  <div class="icon-bar" onclick="Show()">
    <i></i>
    <i></i>
    <i></i>
  </div>

  <ul id="nav-lists">
    <li class="close"><span onclick="Hide()">X</span></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Blog</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>

</div>

CSS:

      *::before,
  *::after {
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
  }

  .container {
    height: 60px;
    background-color: #333333;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    overflow: hidden;    
  }
  
  .container .logo {
    max-width: 250px;
    padding: 0 10px;
    overflow: hidden;
  }
  
  .container .logo a {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    height: 60px;
  }
  
  .container .logo a img {
    max-width: 100%;
    max-height: 60px;
  }
  
  .container .navbar {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    padding-bottom: 5px;
  } 
  .container .navbar ul {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    list-style: none;
    margin: 0;
    padding: 0;
  }  
  .container .navbar ul li a {
    text-decoration: none;
    color: #999999;
    font-size: 14px;
    text-transform: uppercase;
    display: block;
    height: 60px;
    line-height: 60px;
    cursor: pointer;
    padding: 0 10px;
  } 
  .container .navbar ul li a:hover {
    color:#4B088A;
    background-color: rgba(23, 23, 23, 0.9);
  }
  .container .navbar ul .close {
    display: none;
    text-align: right;
    padding: 10px;
  }
  .container .navbar ul .close span {
    font-size: 40px;
    display: inline-block;
    border: 1px solid #cccccc;
    padding: 0 10px;
    cursor: pointer;
  }  
  .container .navbar .icon-bar {
    padding: 18px 8px;
    width: 50px;
    height: 60px;
    display: none;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    cursor: pointer;
  }  
  .container .navbar .icon-bar i {
    background-color: #ffffff;
    height: 2px;
  }
  
  @media only screen and (max-width: 650px) {
   .container {
      -webkit-box-pack: justify;
      -ms-flex-pack: justify;
      justify-content: space-between;
      touch-action: none;
      overflow: hidden;
    }
    .container .logo {
      -webkit-box-flex: 1;
      -ms-flex: 1;
      flex: 1;
    }
    .container .navbar {
      -webkit-box-flex: 0;
      -ms-flex: 0;
      flex: 0;     
    }
    .container .navbar ul {
      -ms-flex-wrap: nowrap;
      flex-wrap: nowrap;
      position: fixed;
      left: 100%;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -ms-flex-direction: column;
      flex-direction: column;
      background: rgb(32, 21, 30);
      width: 100%;
      height: 100%;
      overflow: auto;
      -webkit-transition: left .3s;
      -o-transition: left .3s;
      transition: left .3s;
    }
    .container .navbar ul li a {
      padding: 10px;
      font-size: 16px;
      height: auto;
      line-height: normal;
      color: #555555;
    }
    .container .navbar ul .close {
      display: block;
    }
    .container .navbar .icon-bar {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
    }
    .container .navbar ._Menus-show {
      left: 0;
    }
    @media screen and (max-width:875px) {
      .navbar.responsive {
        position: fixed;
        width: 100%;
        height: 100px;
        background-color: rgba(236, 201, 205, 1);
        transition: background-color .6s;
   
      }
    }
}

还有 JavaScript!

     var navList = document.getElementById("nav-lists");
  const modal = document.querySelector("#modal");

var body = body;

function Show() {
  navList.classList.add("_Menus-show");
  body.style.overflow = "hidden";

}

function Hide() {
  navList.classList.remove("_Menus-show");
  body.style.overflow = "auto";
  

}

总结:我想在导航栏打开并禁用滚动时隐藏正文上的元素。请查看 Gif 以获得更好的理解。

抱歉英语不好,提前致谢!

【问题讨论】:

  • var body = body; 在您的 Javascript 中无效。

标签: javascript html css mobile navigation


【解决方案1】:

为你的身体标签名称“body”添加一个 id

尝试这样做

<body id="body">
..Your code goes here
</body>

<script>
var body = document.getElementById("body");
 // your javascript
</script>

【讨论】:

    【解决方案2】:

    你链接的这段代码很小(我的意思是试试这段代码,它可以像你想要的那样工作)。从 gif 我认为问题出在 css 中。尝试将position: absoulte/fixed; z-index: 1 添加到navbar。在js中试试这个解决方案:

    let isNavbarActive = false;
    document.querySelector('ul.nav-lists').addEventListener('click', ()=> { 
       isNavbarActive = !isNavbarActive
       if (isNavbarActive) {
          document.body.style.overflowY = 'hidden';
          navList.classList.add("_Menus-show");
       } else {
          navList.classList.remove("_Menus-show");
          document.body.style.overflowY = '';
       }
    })
    

    【讨论】:

    • 适用于禁用滚动!但是元素问题还是存在
    【解决方案3】:

    您的var body = body 有问题。你不能像这样获取 html 正文。 你应该像这样获取 body 元素:let body = document.querySelector('body')

    你的 Show 功能没问题。

    【讨论】:

      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多