我正在使用Bootstrap Modal处理两个不同的事情。第一种是从MVC局部视图中动态加载它们。那部分似乎有效。现在我正试图让Modal集中在页面上。
我正在使用这个例子,但似乎无法让它工作。 https://www.abeautifulsite.net/vertically-centering-bootstrap-modals
我将JavaScript中对话框ID的名称更改为“.modal-container”,以便找到我的模态。除此之外,我没有看到任何其他东西,我需要改变。我看过F12开发工具,但没有看到任何错误。
Index.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/Site.css" rel="stylesheet" />
</head>
<body>
<div>
<div class="container">
@Html.ActionLink("Test Modal ", "TestModal", "Test", null, new { @class = "modal-link btn btn-warning" })
</div>
</div>
<div id="modal-container" class="modal fade" role="dialog">
<div class="modal-content">
</div>
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script type="text/javascript">
//**** Bootstrap Modal - used with Partial Views ***********************
$(function () {
// Attach modal-container bootstrap attributes to links with .modal-link class.
// when a link is clicked with these attributes, bootstrap will display the href content in a modal dialog.
$('body').on('click', '.modal-link', function (e) {
e.preventDefault();
$(this).attr('data-target', '#modal-container');
$(this).attr('data-toggle', 'modal');
});
// Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears
$('body').on('click', '.modal-close-btn', function () {
$('#modal-container').modal('hide');
});
//clear modal cache, so that new content can be loaded
$('#modal-container').on('hidden.bs.modal', function () {
$(this).removeData('bs.modal');
});
$('#CancelModal').on('click', function () {
return false;
});
});
/**
* Vertically center Bootstrap 3 modals so they aren't always stuck at the top
*/
$(function () {
function reposition() {
var modal = $(this),
dialog = modal.find('.modal-container');
modal.css('display', 'block');
// Dividing by two centers the modal exactly, but dividing by three
// or four works better for larger screens.
dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height()) / 2));
}
// Reposition when a modal is shown
$('.modal').on('show.bs.modal', reposition);
// Reposition when the window is resized
$(window).on('resize', function () {
$('.modal:visible').each(reposition);
});
});
</script>
</body>
</html>
控制器
public class TestController : Controller
{
// GET: Test
public ActionResult Index()
{
return View();
}
public ActionResult TestModal()
{
return PartialView("_TestModal");
}
}
_TestModal.cshtml
<div class="modal-body">
<h4>
Are you sure you want to delete this cost center from all clocks in the group?
</h4>
@using (Html.BeginForm("Lyubomir", "Home", FormMethod.Post))
{
<div class="row">
</div>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" id="approve-btn" class="btn btn-danger">Save</button>
</div>
</div>
}
</div>
<script type="text/javascript">
$(function () {
$('#approve-btn').click(function () {
$('#modal-container').modal('hide');
});
});
</script>
编辑:我应该在最初的帖子中提到Modal只显示在页面顶部并且宽度达到100%。
答案 0 :(得分:2)
好吧,我注意到了(我相信的是)你的代码中的一个相当简单的错误:
dialog = modal.find('.modal-container');
找到任何具有CLASS为'modal-container'的元素。
但是,在您的代码段中,您将div的ID设置为“modal-container”,而不是CLASS。
因此上面的代码行没有找到你想要的div。事实上,我认为它没有找到任何元素(因为没有一类'模态容器')。
所以也许可以改为:
<div class="modal fade modal-container" role="dialog">
<div class="modal-content">
</div>
</div>
OR 让HTML保持不变,并将modal.find()更改为:
dialog = modal.find('#modal-container');
如果有帮助,请告诉我。
修改强>
你的模态是全宽的,因为你的Bootstrap模态结构是关闭的。你错过了一个div。
这就是你所拥有的:
<div id="modal-container" class="modal fade" role="dialog">
<div class="modal-content">
</div>
</div>
这是你需要的(并且应该解决你的问题):
<div id="modal-container" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
使用'modal-dialog'类添加div应该有帮助。
哦,并将modal.find()更改为:
dialog = modal.find('.modal-dialog');