简体   繁体   中英

bootstrap 4 modal-backdrop style (specific modal)

I wondered how can I change the colour of the modal backdrop (not the background colour of modal) for specific 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

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");
})

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

Use div modal-content instead of modal-backdrop, like this :

$(".modal-content").css('background', 'red');

UPDATE :

Add in your css :

.modal-backdrop.show{background-color: red;}

If I am understanding your question correctly, I think you can change your CSS to this:

.modal-content {
  background-color:red;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM