当Overlay处于活动状态时,网页的滚动应该是可见的

时间:2017-04-05 21:42:34

标签: javascript html css overlay

目标:
当您打开叠加层时,网页的滚动(不是叠加的滚动)应该是可见的,但不能激活使用。

问题:
当您看不到滚动时,看到页面移动到右侧,当您看到滚动页面移动到与叠加相关的右侧时,会感到恼火。

有办法吗?

谢谢!



function openNav() {
    document.getElementById("myNav").style.height = "100%";
    
     $('body').addClass('noscroll');   
}

function closeNav() {
    document.getElementById("myNav").style.height = "0%";
    
     $('body').removeClass('noscroll');
    
}

body {
    margin: 0;
    font-family: 'Lato', sans-serif;
}

.overlay {
    height: 0%;
    width: 100%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.8);
    overflow-y: hidden;
    transition: 0.5s;
}

.overlay-content {
   position: relative;
   top: 10%;
   width: 70%;
   text-align: left;
   margin: 30px auto;
   background: white;
    overflow-y: auto;
    height: 100%;  
    
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}


.noscroll { 
  overflow: hidden;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
    
    <a href="#">About</a>
    <a href="#">Services</a>        
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">CANDY</a>    
  </div>
</div>

<h2>Fullscreen Overlay Nav Example</h2>
<p>Click on the element below to open the fullscreen overlay navigation menu.</p>
<p>In this example, the navigation menu will slide downwards from the top:</p>
  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
   
<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>  

<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

添加&#34;位置:固定;溢出-γ:滚动&#34;你的无滚动课。

如果您不希望它转到顶部,请根据正文的当前滚动条获取当前的X位置,并将Y偏移设置为

&#13;
&#13;
function openNav() {
    document.getElementById("myNav").style.height = "100%";
    $(document).scrollTop()
     $('body').addClass('noscroll');   

}

function closeNav() {
    document.getElementById("myNav").style.height = "0%";
    
     $('body').removeClass('noscroll');
$('body').scroll(function() { alert("scroll") });
    
}
&#13;
body {
    margin: 0;
    font-family: 'Lato', sans-serif;
    overflow-y: scroll;

}

.overlay {
    height: 0%;
    width: 100%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.8);
    overflow-y: hidden;
    transition: 0.5s;
}

.overlay-content {
   position: relative;
   top: 10%;
   width: 70%;
   text-align: left;
   margin: 30px auto;
   background: white;
    overflow-y: auto;
    height: 100%;  
    
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}


.noscroll { 
  position: fixed; overflow-y:scroll;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
    
    <a href="#">About</a>
    <a href="#">Services</a>        
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">CANDY</a>    
  </div>
</div>

<h2>Fullscreen Overlay Nav Example</h2>
<p>Click on the element below to open the fullscreen overlay navigation menu.</p>
<p>In this example, the navigation menu will slide downwards from the top:</p>
  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
   
<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>  

<br/>    
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>    
  <br/>  
<br/>  
<br/>  
<br/>  
<br/>  
<br/>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试此操作(点击时滚动将显示在顶部但将被禁用):

function noscroll() {
  window.scrollTo( 0, 0);
}

function openNav() {
   document.getElementById("myNav").style.height = "100%";
   window.addEventListener('scroll', noscroll);  
}

function closeNav() {
   document.getElementById("myNav").style.height = "0%";
  window.removeEventListener('scroll', noscroll)
}