【问题标题】:How to turn off if the mobile navbar is open when the page is scrolled? [closed]如果页面滚动时移动导航栏打开,如何关闭? [关闭]
【发布时间】:2020-05-07 02:42:33
【问题描述】:

如果导航栏在网站的移动视图中是打开的,如何在页面滚动时关闭导航栏?最简单的方式(css或js)

【问题讨论】:

    标签: javascript html jquery css bootstrap-4


    【解决方案1】:

    对我来说,我更喜欢使用 JS。 这是我从w3schools 得到的一个例子,它适用于导航栏的网络和移动视图

    var prevScrollpos = window.pageYOffset;
    window.onscroll = function() {
      var currentScrollPos = window.pageYOffset;
      if (prevScrollpos > currentScrollPos) {
        document.getElementById("navbar").style.top = "0";
      } else {
        document.getElementById("navbar").style.top = "-50px";
      }
      prevScrollpos = currentScrollPos;
    }
    body {
      margin: 0;
      background-color: #f1f1f1;
      font-family: Arial, Helvetica, sans-serif;
    }
    
    #navbar {
      background-color: #333;
      position: fixed;
      top: 0;
      width: 100%;
      display: block;
      transition: top 0.3s;
    }
    
    #navbar a {
      float: left;
      display: block;
      color: #f2f2f2;
      text-align: center;
      padding: 15px;
      text-decoration: none;
      font-size: 17px;
    }
    
    #navbar a:hover {
      background-color: #ddd;
      color: black;
    }
    <body>
    
    <div id="navbar">
      <a href="#home">Home</a>
      <a href="#news">News</a>
      <a href="#contact">Contact</a>
    </div>
    
    <div style="padding:15px 15px 2500px;font-size:30px;margin-top:30px;">
      <p><b>This example demonstrates how to hide a navbar when the user starts to scroll the page.</b></p>
      <p>Scroll down this frame to see the effect!</p>
      <p>Scroll up to show the navbar.</p>
      <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
    
    </body>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多