gpt4 book ai didi

javascript - 当覆盖处于事件状态时,网页的滚动应该可见

转载 作者:行者123 更新时间:2023-11-28 04:00:24 24 4
gpt4 key购买 nike

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

问题:
当您看到滚动条不可见时页面移动到右侧,而当您看到滚动条时页面移动到与叠加层相关的右侧,这让人很恼火。

有办法吗?

谢谢!

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/>

最佳答案

只需将“position: fixed; overflow-y:scroll”添加到你的无滚动类。

如果你不希望它到顶部,根据主体的当前滚动获取当前的 X 位置并将 Y 偏移设置为该值

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") });

}
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;
}
<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/>

关于javascript - 当覆盖处于事件状态时,网页的滚动应该可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43242138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com