简体   繁体   中英

Block background from scrolling when open a modal

I've been trying to solve this problem and I tried like almost everything I found on google but I did not find a solution. How can I block the background from scrolling when the modal is opened? What I've tried works on Desktop version but when I do a localhost and test it on both iOS and Android does not work and it keeps scrolling.

I tried to add block the background with jquery by hiding the body but this is not working.Another one that I tried is adding body.modal-open{overflow:hidden;}. Any help would be welcomed, thanks.

 $("#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> 

Since you are using jQuery, simply use the CSS overflow: hidden; property on the entire body , that way the whole body will not scroll instead of your .noscroll class, and then set the overflow property of your pop-up modal to overflow: auto; , that way the modal's scrolling will not be disabled. But you will also need to use position: fixed; because without it, scrolling is still possible on mobile devices. And after the pop-up is closed, just reset the original CSS properties for proper functionality:

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

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM