When modal appears, it will trigger event show.bs.modal
before appearing. I tried at Safari 13.1.2
on MacOS 10.15.6
. When show.bs.modal
event triggered, the .modal-backgrop
is not inserted into body
yet.
So, I give up to addClass
, and removeClass
to .modal-backdrop
dynamically.
After viewing a lot articles on the Internet, I found a code snippet. It addClass
and removeClass
to the body
, which is the parent of .modal-backdrop
, when show.bs.modal
and hide.bs.modal
events triggered.
ps: I use Bootstrap 4.5.
// In order to addClass/removeClass on the `body`. The parent of `.modal-backdrop`
.no-modal-bg .modal-backdrop {
background: none;
}
$('#myModalId').on('show.bs.modal', function(e) {
$('body').addClass('no-modal-bg');
}).on('hidden.bs.modal', function(e) {
// 'hide.bs.modal' or 'hidden.bs.modal', depends on your needs.
$('body').removeClass('no-modal-bg');
});