gpt4 book ai didi

javascript - 停止和启动页面溢出/单击时滚动的能力

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:44 28 4
gpt4 key购买 nike

背景:
我使用教程 here 创建了一个 (HTML、CSS、JS) 灯箱.当显示灯箱覆盖 div 时(单击按钮时),我想停止滚动条功能。这样,当有人在页面上查看灯箱中的图像时,他们无法滚动到页面的其他部分。然后,当灯箱关闭时,再次启用滚动条功能,以便它们可以自由滚动。不过,我似乎找不到正确的方法。

我试过这个:

$(document).css('overflow-y', 'hidden');

但是当我将它添加到我的代码中时没有任何反应。就好像我从未添加过它一样。

问题:
有没有办法在灯箱打开时停止页面溢出功能,然后在关闭时重新启用它?

抱歉,如果有一个简单的答案。我是 JavaScript 新手。

Javascript:

function startLightBox() {
var lbBg = document.getElementById("lightBoxBg");
var lb = document.getElementById("lightBox");
var screenTop = $(document).scrollTop();
var currentMid = $(document).scrollTop() + 250;

$('#lightBoxBg').css('top', screenTop);
$('#lightBox').css('top', currentMid);

lbBg.style.display = "block";
lb.style.display = "block";
}

function dismiss() {
var lbBg = document.getElementById("lightBoxBg");
var lb = document.getElementById("lightBox");
var screenTop = $(document).scrollTop();
var currentMid = $(document).scrollTop() + 250;


$('#lightBoxBg').css('top', screenTop);
$('#lightBox').css('top', currentMid);
lbBg.style.display = "none";
lb.style.display = " none";
}

CSS:

#lightBoxBg {
position: absolute;
top: 0px;
left: 0px;
opacity: 0.5;
display: none;
background-color: #000000;
height: 100%;
width: 100%;
}

#lightBox {
position: absolute;
top: 50%;
left: 40%;
display: none;
}

HTML 正文

<body>

<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
<iframe width="560" height="315" src="https://www.youtube.com/embed/b6JnMZL3wGs" frameborder="0" allowfullscreen></iframe>
</div>
<div class="col-lg-6">
<iframe width="560" height="315" src="https://www.youtube.com/embed/b6JnMZL3wGs" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<iframe width="560" height="315" src="https://www.youtube.com/embed/b6JnMZL3wGs" frameborder="0" allowfullscreen></iframe>
</div>
<div class="col-lg-6">
<iframe width="560" height="315" src="https://www.youtube.com/embed/b6JnMZL3wGs" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<iframe width="560" height="315" src="https://www.youtube.com/embed/b6JnMZL3wGs" frameborder="0" allowfullscreen></iframe>
</div>
<div class="col-lg-6">
<iframe width="560" height="315" src="https://www.youtube.com/embed/b6JnMZL3wGs" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<iframe width="560" height="315" src="https://www.youtube.com/embed/b6JnMZL3wGs" frameborder="0" allowfullscreen></iframe>
</div>
<div class="col-lg-6">
<iframe width="560" height="315" src="https://www.youtube.com/embed/b6JnMZL3wGs" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<div class="row">
<div class="col-lg-12">

<button onclick="startLightBox()">Light Box</button>

</div>

</div>
</div>

<div id="lightBoxBg" onclick="dismiss()">

</div>
<div id="lightBox">
<img src="test2.png" />
</div>
</body>

最佳答案

试试这个-

jquery

startLightBox() 函数中使用 $("body").css("overflow", "hidden");

并在 dismiss() 函数中使用 $("body").css("overflow", "auto");

Javascript

使用

document.documentElement.style.overflow = 'hidden';  // firefox, chrome
document.body.scroll = "no"; // ie only

startLightBox() 函数中。

并使用

document.documentElement.style.overflow = 'hidden';  // firefox, chrome
document.body.scroll = "no"; // ie only

dismiss() 函数中。

关于javascript - 停止和启动页面溢出/单击时滚动的能力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436478/

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