gpt4 book ai didi

javascript - 打开模式时阻止背景滚动

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

我一直在努力解决这个问题,我尝试了几乎所有我在谷歌上找到的东西,但我没有找到解决方案。打开模式时如何阻止背景滚动?我尝试过的方法适用于桌面版本,但是当我执行本地主机并在 iOS 和 Android 上对其进行测试时,它不起作用并且它一直在滚动。

我尝试通过隐藏正文来使用 jquery 添加 block 背景,但这不起作用。我尝试的另一个方法是添加 body.modal-open{overflow:hidden;}。欢迎任何帮助,谢谢。

$("#popUpWindow").on("show", function () {
$("body").addClass("noscroll");
}).on("hidden", function () {
$("body").removeClass("noscroll")
});
#popUpWindow{
background: lightblue;
}

body.modal-open{
overflow: hidden;
}

.noscroll { overflow: hidden;}
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
<div class="container">

<h2>Simulacron 3 is the story of a virtual city (total environment simulator) for marketing research, developed by a scientist to reduce the need for opinion polls. The computer-generated city simulation is so well-programmed, that, although the inhabitants have their own consciousness, they are unaware, except for one, that they are only electronic impulses in a computer.

The simulator's lead scientist, Hannon Fuller, dies mysteriously, and a co-worker, Morton Lynch, vanishes. The protagonist, Douglas Hall, is with Lynch when he vanishes, and Hall subsequently struggles to suppress his inchoate madness. As time and events unwind, he progressively grasps that his own world is probably not "real" and might be only a computer-generated simulation.</h2>
<h2>Simulacron 3 is the story of a virtual city (total environment simulator) for marketing research, developed by a scientist to reduce the need for opinion polls. The computer-generated city simulation is so well-programmed, that, although the inhabitants have their own consciousness, they are unaware, except for one, that they are only electronic impulses in a computer.

The simulator's lead scientist, Hannon Fuller, dies mysteriously, and a co-worker, Morton Lynch, vanishes. The protagonist, Douglas Hall, is with Lynch when he vanishes, and Hall subsequently struggles to suppress his inchoate madness. As time and events unwind, he progressively grasps that his own world is probably not "real" and might be only a computer-generated simulation.</h2>
<h2>Simulacron 3 is the story of a virtual city (total environment simulator) for marketing research, developed by a scientist to reduce the need for opinion polls. The computer-generated city simulation is so well-programmed, that, although the inhabitants have their own consciousness, they are unaware, except for one, that they are only electronic impulses in a computer.

The simulator's lead scientist, Hannon Fuller, dies mysteriously, and a co-worker, Morton Lynch, vanishes. The protagonist, Douglas Hall, is with Lynch when he vanishes, and Hall subsequently struggles to suppress his inchoate madness. As time and events unwind, he progressively grasps that his Simulacron 3 is the story of a virtual city (total environment simulator) for marketing research, developed by a scientist to reduce the need for opinion polls. The computer-generated city simulation is so well-programmed, that, although the inhabitants have their own consciousness, they are unaware, except for one, that they are only electronic impulses in a computer.

The simulator's lead scientist, Hannon Fuller, dies mysteriously, and a co-worker, Morton Lynch, vanishes. The protagonist, Douglas Hall, is with Lynch when he vanishes, and Hall subsequently struggles to suppress his inchoate madness. As time and events unwind, he progressively grasps that his own world is probably not "real" and might be only a computer-generated simulation.own world is probably not "real" and might be only a computer-generated simulation.</h2>
<h2>Simulacron 3 is the story of a virtual city (total environment simulator) for marketing research, developed by a scientist to reduce the need for opinion polls. The computer-generated city simulation is so well-programmed, that, although the inhabitants have their own consciousness, they are unaware, except for one, that they are only electronic impulses in a computer.

The simulator's lead scientist, Hannon Fuller, dies mysteriously, and a co-worker, Morton Lynch, vanishes. The protagonist, Douglas Hall, is with Lynch when he vanishes, and Hall subsequently struggles to suppress his inchoate madness. As time and events unwind, he progressively grasps that his Simulacron 3 is the story of a virtual city (total environment simulator) for marketing research, developed by a scientist to reduce the need for opinion polls. The computer-generated city simulation is so well-programmed, that, although the inhabitants have their own consciousness, they are unaware, except for one, that they are only electronic impulses in a computer.

The simulator's lead scientist, Hannon Fuller, dies mysteriously, and a co-worker, Morton Lynch, vanishes. The protagonist, Douglas Hall, is with Lynch when he vanishes, and Hall subsequently struggles to suppress his inchoate madness. As time and events unwind, he progressively grasps that his own world is probably not "real" and might be only a computer-generated simulation.own world is probably not "real" and might be only a computer-generated simulation.</h2>


<button type='button' class="btn btn-success" data-toggle="modal" data-target="#popUpWindow">Open Log In Window</button>

<div class="modal fade" id="popUpWindow">
<div class="modal-dialog">
<div class="modal-content">
<!-- header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h3 class="modal-title">Login Form</h3>
</div>
<!-- body -->
<div class="modal-header">
<form role="form">
<div class="form-group">
<input type="email" class="form-control" placeholder="Email"/>
<input type="password" class="form-control" placeholder="Password" />
</div>
</form>
</div>
<!-- footer -->
<div class="modal-footer">
<button class="btn btn-primary btn-block">Log In</button>
</div>

</div>
</div>
</div>

</div>

最佳答案

由于您使用的是 jQuery,只需在整个 body 上使用 CSS overflow: hidden; 属性,这样整个 body 就会不要滚动而不是你的 .noscroll 类,然后将弹出模式的 overflow 属性设置为 overflow: auto;,这样模式的滚动不会被禁用。但是您还需要使用 position: fixed; 因为没有它,仍然可以在移动设备上滚动。在弹出窗口关闭后,只需重置原始 CSS 属性即可实现正常功能:

$("#popUpWindow").on("show", function () {
$("body").css("overflow", "hidden");
$("body").css("position", "fixed");
$("#popUpWindow").css("overflow", "auto");
}).on("hidden", function () {
$("body").css("overflow", "auto");
$("body").css("position", "initial");
});

希望对您有所帮助!

关于javascript - 打开模式时阻止背景滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52997479/

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