admin管理员组文章数量:1315941
I am looking for a way to make this modal persistent once it show up. As it's presented here, the user can close it with a simple click outside of the 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=".3.6/css/bootstrap.min.css">
<script src=".12.0/jquery.min.js"></script>
<script src=".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>
I am looking for a way to make this modal persistent once it show up. As it's presented here, the user can close it with a simple click outside of the 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./bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis./ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn./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>
Is there a way to block this modal so it's still there even with a mouse click outside of it?
Share Improve this question edited Jan 28, 2016 at 17:26 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Jan 28, 2016 at 16:48 farhawafarhawa 10.4k16 gold badges56 silver badges92 bronze badges 03 Answers
Reset to default 6To "persist the modal" (aka prevent hiding) hook into the hide event and return false to prevent hiding.
$("#myModal").on('hide.bs.modal', function () {
return false
});
Here's a full code example.
<!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./bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis./ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn./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>
Edit: To hide the modal, redefine the response to the hide event before calling hide()
. The following function does this. You can execute the function whenever is appropriate (e.g. on a button click).
function hideMyModal () {
$("#myModal").on('hide.bs.modal', function () { });
$("#myModal").hide()
}
Something like this ought to get you started. You can fiddle around with different values like color, timing, etc.
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>
Is code-only answer acceptable?
BootstrapDialog.show({
title: 'Modal Header',
message: 'Some text in the modal.',
closeByBackdrop: false // <- This prevents closing the dialog by clicking outside
});
<script src="https://code.jquery./jquery-2.1.4.min.js"></script>
<link href="https://maxcdn.bootstrapcdn./bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare./ajax/libs/bootstrap3-dialog/1.34.9/css/bootstrap-dialog.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare./ajax/libs/bootstrap3-dialog/1.34.9/js/bootstrap-dialog.js"></script>
More examples please see here http://nakupanda.github.io/bootstrap3-dialog/
本文标签: javascriptHow can I make this modal persistentStack Overflow
版权声明:本文标题:javascript - How can I make this modal persistent? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741991350a2409166.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论