我正在寻找一种方法,一旦它出现就让这个模态持久化。如此处所示,用户只需在div外单击即可关闭它。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$(window).load(function(){
$('#myModal').modal('show');
});
});
</script>
</body>
</html>
有没有办法阻止这个模态,所以即使在鼠标外面点击它仍然存在?
答案 0 :(得分:2)
坚持模式&#34; (又名防止隐藏)钩入hide事件并返回false以防止隐藏。
$("#myModal").on('hide.bs.modal', function () {
return false
});
这是一个完整的代码示例。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Persistent Modal</h2>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">My Persistent Modal</h4>
</div>
<div class="modal-body">
<p>I'm here for good. Can't hide me.</p>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$(window).load(function(){
$('#myModal').modal('show');
});
$("#myModal").on('hide.bs.modal', function () {
return false
});
});
</script>
</body>
</html>
&#13;
修改:要隐藏模态,请在调用hide()
之前重新定义对隐藏事件的响应。以下功能可以做到这一点。您可以在适当的时候执行该功能(例如,单击按钮)。
function hideMyModal () {
$("#myModal").on('hide.bs.modal', function () { });
$("#myModal").hide()
}
答案 1 :(得分:1)
这样的事情应该让你开始。你可以摆弄颜色,时间等不同的值。
html, body {
margin: 0;
background: #878787;
}
h2 {
text-align: center;
}
.modal-content {
position: absolute;
top: 10px;
animation: modal 300ms linear;
border: 1px solid black;
border-radius: 5px;
text-align: center;
background: #fff;
width: 90%;
left: 10px;
box-shadow: 2px 2px 1px #5f5f5f;
opacity: 0;
animation-fill-mode: forwards;
animation-delay: 500ms;
}
@keyframes modal {
0% { opacity: 0; top: 0px}
5% { z-index: 2; }
100% { opacity: 1; z-index: 2; top: 10px}
}
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
</div>
</div>
</div>
</div>
答案 2 :(得分:1)
仅代码答案是否可以接受?
BootstrapDialog.show({
title: 'Modal Header',
message: 'Some text in the modal.',
closeByBackdrop: false // <- This prevents closing the dialog by clicking outside
});
&#13;
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.9/css/bootstrap-dialog.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.9/js/bootstrap-dialog.js"></script>
&#13;