Bootstrap: Open Another Modal in Modal

后端 未结 21 1010
难免孤独
难免孤独 2020-11-27 11:28

So, I\'m using this code to open another modal window in a current opened modal window:



        
21条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 11:32

    This thread is old, but for those who come from google, Ive come with a solutions that is hybrid from all the answers Ive found on the net.

    This will make sure level class is being added:

    $(document).on('show.bs.modal', '.modal', function (event) {
      $(this).addClass(`modal-level-${$('.modal:visible').length}`);
    });
    

    Inside my SCSS Ive wrote a rule that supports main modal and 10 on top (10 because from z-index: 1060 popover takes place), you can add levels count inside _variables.scss if you want:

    @for $level from 0 through 10 {
      .modal-level-#{$level} {
        z-index: $zindex-modal + $level;
    
        & + .modal-backdrop {
          z-index: $zindex-modal + $level - 1;
        }
      }
    }
    

    Do not forget that you cannot have modal inside modal as their controls will be messed up. In my case all my modals were at the end of body.

    And finally as members below also mentions this, after closing one modal, you need to keep modal-open class on body:

    $(document).on('hidden.bs.modal', function (e) {
      if ($('.modal:visible').length > 0) {
        $('body').addClass('modal-open');
      }
    });
    

提交回复
热议问题