Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 778
demoncoder Avatar asked Apr 10 '18 13:04

demoncoder


1 Answers

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

like image 134
Zim Avatar answered Oct 22 '22 21:10

Zim