Scrolling issues with multiple bootstrap modals

后端 未结 6 2042
夕颜
夕颜 2020-12-29 02:54

I have a page with a modal with a lot of info so you need to scroll. This modal contains a link to a second modal.

When I

  • open modal 1
  • click o
相关标签:
6条回答
  • 2020-12-29 03:16
       $(document).on('hidden.bs.modal', '.modal', function () {
            $('.modal:visible').length && $(document.body).addClass('modal-open');
       });
    
    0 讨论(0)
  • 2020-12-29 03:18

    I tried the previous solutions, and not working for my use case:

    1. just adding css .modal { overflow: auto !important; } This will cause the content below modal become scrollable. Not good when you want to keep the previous content position. especially in mobile browser.
    2. Use javascript to track opened modal and keep the 'modal-open' class in body element using this jQuery selector $('.modal:visible').length. This is not working when there are multiple modal opened and closed fast. I use BS modal for my ajax spinner, so the $('.modal:visible') is not accurate.

    This is the working one for me:

    1. Use JS global variable to keep track/count of opened modal; instead of just using :visible selector
    2. And I added css style so I don't have to recalculate backdrop z-index https://stackoverflow.com/a/63473738/423356

    var bootstrapModalCounter = 0; //counter is better then using visible selector
    
        $(document).ready(function () {  
          $('.modal').on("hidden.bs.modal", function (e) {
            --bootstrapModalCounter;
            if (bootstrapModalCounter > 0) {
              //don't need to recalculate backdrop z-index; already handled by css
              //$('.modal-backdrop').first().css('z-index', parseInt($('.modal:visible').last().css('z-index')) - 10);
              $('body').addClass('modal-open');
            }
          }).on("show.bs.modal", function (e) {
            ++bootstrapModalCounter;
            //don't need to recalculate backdrop z-index; already handled by css
          });
        });
    .modal { 
    /*simple fix for multiple bootstrap modal backdrop issue: 
    don't need to recalculate backdrop z-index;
    https://stackoverflow.com/questions/19305821/multiple-modals-overlay/21816777 */
      background: rgba(0, 0, 0, 0.5);
    }

    This is modified fiddle from @Keeleon for the fix https://jsfiddle.net/4m68uys7/

    This is github issue https://github.com/nakupanda/bootstrap3-dialog/issues/70

    0 讨论(0)
  • 2020-12-29 03:19

    The solution that worked for me was:

    $('.modal').on("hidden.bs.modal", function (e) { 
        if ($('.modal:visible').length) { 
            $('body').addClass('modal-open');
        }
    });
    
    0 讨论(0)
  • 2020-12-29 03:25

    this is the solution:

    <script>
        $('#id_ofyou_secondary_modal').on('hidden.bs.modal', function (e) {
    
          $('body').addClass('modal-open');
    
        });
    </script>
    

    take care that "#idofyousecondarymodal" is the id of the secondary or tertiary or infinite modal. but NEVER write the ID of the first modal.

    example i have 2 modal:

    <div id="mymodal1" class="modal fade in" style="display:none;">
    .
    .
    .
    .
    </div>
    <div id="mymodal2" class="modal fade in" style="display:none;">
    .
    .
    .
    .
    </div>   
    

    then the script will be:

    <script>
        $('#mymodal2').on('hidden.bs.modal', function (e) {
    
          $('body').addClass('modal-open');
    
        });
    </script>
    

    jus add this code and work fine.

    0 讨论(0)
  • 2020-12-29 03:35

    Add following to your CSS :

    .modal
    {
      overflow: scroll !important;
    }
    
    0 讨论(0)
  • 2020-12-29 03:41

    Add

    .modal { overflow: auto !important; }
    

    To your CCS.

    Without your code I went ahead and created this jsFiddle that recreates your issue, or at least a very similar one. Add your code and I will test if this works or not.

    Adding that line to the CSS fixed the issue as demonstrated with this jsFiddle.

    Solution taken from this thread on github which offers other solutions as well.

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