我在项目中使用了Bootstrap的模态弹出窗口,并希望实现以下功能:
- 打开模态弹出窗口后,点击背景不应该关闭弹出窗口。
- 打开模态弹出窗口时,背景不应该发生模糊,即打开模态弹出窗口不应该对背景产生任何影响。
- 打开模态弹出窗口后,用户也可以在背景上工作,此时弹出窗口不应该关闭。
我在项目中使用了Bootstrap的模态弹出窗口,并希望实现以下功能:
1) 打开模型弹出窗口并单击背景时,不应关闭弹出窗口。
在模态定义本身中包括数据属性data-keyboard="false" data-backdrop="static"
:
<div class="modal fade" id="myModal" role="dialog" data-keyboard="false" data-backdrop="static">
// Modal HTML Markup
</div>
2) 当打开模态弹窗时,背景不应该模糊。意思是,打开模态弹窗时,背景不应受到任何影响。
将 .modal-backdrop
的属性值设置为 display:none;
。
.modal-backdrop {
display:none;
}
3)打开模型弹出窗口后,用户还可以在后台工作,此时弹出窗口不应关闭。
在.modal-open .modal
中添加值。
.modal-open .modal {
width: 300px;
margin: 0 auto;
}
顺便提一下:您可能需要使用媒体查询来根据屏幕尺寸调整模态框的宽度。
免责声明:本回答仅用于演示如何实现所有3个目标。 如果您有多个Bootstrap模态框,则上述更改将影响所有模态框,强烈建议使用自定义选择器。
.modal-backdrop {
display: none !important;
}
.modal-open .modal {
width: 300px;
margin: 0 auto;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal">Open Modal</button>
<br> This is a text and can be selected for copying
<br> This is a text and can be selected for copying
<br> This is a text and can be selected for copying
<br> This is a text and can be selected for copying
<br>
<div id="myModal" class="modal fade" role="dialog" data-backdrop="static">
<div class="modal-dialog modal-sm">
<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>
.modal-open {overflow: auto !important;}
,看看是否可以解决问题。 - Shehary$('#myModal').on('shown.bs.modal', function() {
$(document).off('focusin.modal');
});
backdrop="false" 只会移除背景黑屏,但不允许您在背景元素上执行任何操作。 为了保持背景交互,并将模态框保持在中间位置并具有完整视图,您需要在生成模态框后使用js代码删除'modal'类。 并且需要使用一些自定义的CSS样式。添加一个自定义类到模态框中。
<div class="modal fade custom-modal" id="myModal" role="dialog">
<div class ="modal-dialog" role="document">
<div class ="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header moveable-modal-header"></div>
</div>
</div>
</div>
</div>
//cs styles//
.custom-modal{
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: fit-content;
height: fit-content;
padding: 0 !important;
}
.modal-dialog{margin: 0;}
现在,在填充模态框之后,需要从myModal div中删除'modal'类
function openModal(){
$("#myModal").modal({backdrop:false,show:true});
$("#myModal").removeClass("modal");
//这将使模态框可移动//
$("#myModal .modal-dialog").draggable({
handle: ".moveable-modal-header"
});
}
div
中删除backdrop
类和data-backdrop="static"
。 - Guruprasad J Rao