簡體   English   中英

打開模式時阻止背景滾動

[英]Block background from scrolling when open a modal

我一直在嘗試解決此問題,我嘗試了幾乎所有在google上找到的內容,但沒有找到解決方案。 打開模式時如何阻止背景滾動? 我嘗試過的內容在桌面版本上有效,但是當我在本地主機上進行測試並在iOS和Android上均無法正常運行時,它一直在滾動。

我試圖通過隱藏主體來使用jquery添加背景塊,但這不起作用。我嘗試的另一種做法是添加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,因此只需使用CSS overflow: hidden; 整個body屬性,這樣整個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");
});

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM