bootstrap 4 modal-backdrop 风格(特定模态)

我想知道如何更改特定模态的模态背景颜色(不是模态背景颜色).

I wondered how can I change the colour of the modal backdrop (not the background colour of modal) for specific modal.

如果我使用shown.bs.modal"有一些延迟,可以更改颜色.但我想立即更改背景颜色.希望有人可以提供帮助.谢谢.

The colour can be changed if I use "shown.bs.modal" with some delays. But I want to change the backdrop colour immediately. Hope somebody can help. Thank you.

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

$('#exampleModal').on('show.bs.modal', function (e) {
                $(".modal-backdrop").css('background', '#fb0404');
            })

codepen:https://codepen.io/rae0724/pen/oqmPmY

推荐答案

您可以在模态框打开之前在body中添加一个自定义类,然后在它关闭时移除该类...

You can add a custom class to the body before the modal is opened, and then remove the class when it's closed...

$('#exampleModal').on('show.bs.modal', function (e) {
    $('body').addClass("example-open");
}).on('hide.bs.modal', function (e) {
    $('body').removeClass("example-open");
})

您只需要应用到背景的自定义颜色的 CSS.

The you just need the CSS for the custom color applied to the backdrop.

.example-open .modal-backdrop {background-color:red;}

https://www.codeply.com/go/oNKsVikW6n

相关文章