问题
How can the modal be prevented from closing on a background click and still have the clicks register to work with the rest of the controls on the screen ?
Using the static method
$('#myModal').modal({backdrop: 'static', keyboard: false})
disables the entire background.
Edit : My exact problem is this - I have different buttons on the screen and each of them have to show a popup on double click. Now, when I open a popup, I may also want to open another popup without closing the first one. If I make the backdrop static, clicking other buttons doesn't work and if I don't make it static, opening the second closes the first one. How do I enable opening multiple popups at a time ?
Thanks!
回答1:
Add this in style sheet, make sure it override default bootstrap modal values.
.modal-backdrop.in {
opacity: 0 !important;
z-index: -1050 !important;
}
OR
.modal-backdrop {
display: none !important;
}
Working Example
.modal {
-webkit-transform: translateX(0%) translateY(25%) !important;
-moz-transform: translateX(0%) translateY(25%) !important;
-ms-transform: translateX(0%) translateY(25%) !important;
transform: translateX(0%) translateY(25%) !important;
}
.modal-backdrop {
display: none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal" data-backdrop="static">Open Modal</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal2" data-backdrop="static">Open Modal 2</button>
<label>This is a text and can be selected for copying</label>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div id="myModal2" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">This is Modal Header 2</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Note: Modal position change css is just for demo purpose.
来源:https://stackoverflow.com/questions/33000898/bootstrap-modal-disable-close-on-backdrop-click-without-disabling-the-backgrou