How to change background Opacity when bootstrap modal is open

后端 未结 14 1920
慢半拍i
慢半拍i 2020-12-24 00:37

I am using bootstrap modal. When modal is open background content opacity is not changed by default. I tried changing in js using

function showModal() {
doc         


        
相关标签:
14条回答
  • 2020-12-24 01:07

    The problem with overriding using !important is that you will loose the fade in effect.

    So the actual best trick to change the modal-backdrop opacity without breaking the fadeIn effect and without having to use the shameless !important is to use this :

    .modal-backdrop{
      opacity:0; transition:opacity .2s;
    }
    .modal-backdrop.in{
      opacity:.7;
    }
    

    @sass

    .modal-backdrop{
      opacity:0; transition:opacity .2s;
      &.in{opacity:.7;}
    }
    

    Simple and clean.

    0 讨论(0)
  • 2020-12-24 01:10

    you could utilize bootstrap events:: as

    //when modal opens
    $('#yourModal').on('shown.bs.modal', function (e) {
      $("#pageContent").css({ opacity: 0.5 });
    })
    
    //when modal closes
    $('#yourModal').on('hidden.bs.modal', function (e) {
      $("#pageContent").css({ opacity: 1 });
    })
    
    0 讨论(0)
  • 2020-12-24 01:10

    Just setting height to 100% won't be the solution if you have a background page whose height extends beyond browser height.

    Adding to Bhargavi's answer, this is the solution when you have scrollable page in the background.

    .modal-backdrop.in
    {
        opacity:0.5 !important;
        position:fixed;// This makes it cover the entire scrollable page, not just browser height
        height:100%;
    }
    
    0 讨论(0)
  • 2020-12-24 01:13

    If you are using the less version to compile Bootstrap modify @modal-backdrop-opacity in your variables override file $modal-backdrop-opacity for scss.

    0 讨论(0)
  • 2020-12-24 01:13

    What worked for me, I clear the opacity in .modal.backdrop.in

    .modal-backdrop.in{
        filter:alpha(opacity=50);
        opacity:.5
        }
    

    change it to

    .modal-backdrop.in{
       }
    
    0 讨论(0)
  • 2020-12-24 01:14

    I am assuming you want to set the opacity of the modal background...

    Set the opacity via CSS

    .modal-backdrop
    {
        opacity:0.5 !important;
    }
    

    !important prevents the opacity from being overwritten - particularly from Bootstrap in this context.

    0 讨论(0)
提交回复
热议问题