身体滚动时如何固定我的Sidenav

时间:2018-09-30 00:46:10

标签: html css scroll materialize sidenav

如何在滚动正文时修复sidenav 当我向下滚动时,sidenav底部会变空

enter image description here

1 个答案:

答案 0 :(得分:0)

  var header = $("#menu");
  $(window).scroll(function() {    
    var scroll = $(window).scrollTop();
       if (scroll >0) {
          header.addClass("fixed");
        } else {
          header.removeClass("fixed");
        }
});
 .fixed{
    position: fixed !important;
  }
  .c {
    height: 100vh;
    position: relative;
    padding: 50px;
}
div#menu {
    position: absolute;
    padding: 20px;
    background: #2196F3;
    color: ghostwhite;
    width: 200px;
    z-index: 1;
    height: 100vh;
}
.c {
    height: 100vh;
    padding: 50px;
    margin-left: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width: 100%">
  <div class="nav" id="menu">
    <h2>Menu</h2>
  </div>
  <div class="c">
    Lorem ipsum dolor sit amet, consectetur adipisicing 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.

    Lorem ipsum dolor sit amet, consectetur adipisicing 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.
  </div>
</div>