创建Bootstrap模式时,将.modal-backdrop添加到HTML的末尾以创建背景颜色。我可以通过改变该类的颜色来手动切换颜色,但是我想根据触发的2个模态中的哪一个来切换该类的颜色。
我目前正在使用兄弟选择器来尝试区分我喜欢的颜色,但无论调用哪种模式,它现在都会显示#000。
CSS:
.modal-backdrop {
background-color: #fff;
}
.card-modal ~ .modal-backdrop {
background-color: #000;
}
答案 0 :(得分:2)
$(".modal1").on('shown.bs.modal', function() {
$('.modal-backdrop').css('background', 'red');
});
$(".modal1").on('hidden.bs.modal', function() {
$('.modal-backdrop').css('background', '#000');
});
.wrap {
padding: 15px;
}
h1 {
font-size: 28px;
}
h4,
modal-title {
font-size: 18px;
font-weight: bold;
}
.no-borders {
border: 0px;
}
.body-message {
font-size: 18px;
}
.centered {
text-align: center;
}
.btn-primary {
background-color: #2086c1;
border-color: transparent;
outline: none;
border-radius: 8px;
font-size: 15px;
padding: 10px 25px;
}
.btn-primary:hover {
background-color: #2086c1;
border-color: transparent;
}
.btn-primary:focus {
outline: none;
}
.model1 .modal-backdrop {
background: red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="wrap">
<h1>Bootstrap Modal Example</h1>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal1">
Modal 1
</button>
</div>
<div class="modal fade modal1" tabindex="-1" role="dialog" aria-labelledby="modal1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<!-- Modal Content: begins -->
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header no-borders">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="gridSystemModalLabel"></h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<p class="body-message centered"><strong>Modal 1 here.</strong>
</p>
</div>
</div>
<!-- Modal Content: ends -->
</div>
</div>
<!---------------------->
<div class="wrap">
<h1>Bootstrap Modal Example</h1>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal2">
Modal 2
</button>
</div>
<div class="modal fade modal2" tabindex="-1" role="dialog" aria-labelledby="modal2" aria-hidden="true">
<div class="modal-dialog modal-lg">
<!-- Modal Content: begins -->
<div class="modal-content">
<!-- Modal Body -->
<div class="modal-body">
<div class="body-message">
<h4>Modal 2 here.</h4>
<p>How to change this background colour?</p>
</div>
</div>
</div>
<!-- Modal Content: ends -->
</div>
</div>
<强>更新强>
Bootstrap模态的属性: -
shown.bs.modal
hidden.bs.modal
我正在做的是,每当你尝试打开一个bootstrap模型时,就会添加一个带有类模态背景的div,而我只是将该背景颜色改变为你喜欢的颜色。每当您尝试删除模型时,div都将被删除。所以你需要每次都管理它。根据你的要求,如果你有两个以上的模型,那么每个人的颜色应该是不同的(至少是我所解释的)。