【问题标题】:How can I handle opening multiple modal Bootstrap windows如何处理打开多个模式 Bootstrap 窗口
【发布时间】:2015-09-28 07:39:58
【问题描述】:

从第一个模态中的按钮调用第二个模态。当第二个模型打开和关闭时,第一个模型变为非活动状态。我什至无法移动模态框。

【问题讨论】:

  • Bootstrap 官方doc 明确提到“确保不要在另一个仍然可见的情况下打开一个模态。一次显示多个模态需要自定义代码。”所以从技术上讲,你可以做到,但为此你需要设置一个小提琴来准确显示你所追求的,以便我们调查。
  • 非常感谢@Patel,这真的很有帮助。

标签: javascript jquery asp.net-mvc twitter-bootstrap


【解决方案1】:

This article 解释了如何实现这一目标。

使用hidden.bs.modalshown.bs.modal 事件回调我们可以 跟踪一次打开的模式数量。鉴于这些是 模态而不是对话框,一次只能激活一个模态。这 休息应该被禁用。

我在 body 标签上创建了一个计数器,因为没有更好的地方。每个 显示模态的时间我增加计数器然后减少它 当模态关闭时再次。

使用这个计数器,我可以快速计算模态的 z-index 它的相应背景将高于任何其他 页面上的模态和背景。这满足了'禁用一切 else' 的要求。

设置modal的z-index没问题,但是找到 相应的背景 div 需要搜索带有类的标签 .modal-backdrop。为了确保我只在 最近的一个,我设置了 z-index 并添加了一个.fv-modal-stack 给它上课。然后当打开后续模态时,我会查找所有 没有 fv-modal-stack 类的背景。

这里有一些代码:

<script type='text/javascript'>
    $(document).ready(function() {
        $('#openBtn').click(function(){
            $('#myModal').modal({show:true})
        });

        $('.modal').on('hidden.bs.modal', function( event ) {
            $(this).removeClass( 'fv-modal-stack' );
            $('body').data( 'fv_open_modals', $('body').data( 'fv_open_modals' ) - 1 );
        });

        $( '.modal' ).on( 'shown.bs.modal', function ( event ) {
            // keep track of the number of open modals
            if ( typeof( $('body').data( 'fv_open_modals' ) ) == 'undefined' )
            {
                $('body').data( 'fv_open_modals', 0 );
            }

            // if the z-index of this modal has been set, ignore.
            if ( $(this).hasClass( 'fv-modal-stack' ) )
            {
                return;
            }

            $(this).addClass( 'fv-modal-stack' );
            $('body').data( 'fv_open_modals', $('body').data( 'fv_open_modals' ) + 1 );
            $(this).css('z-index', 1040 + (10 * $('body').data( 'fv_open_modals' )));
            $( '.modal-backdrop' ).not( '.fv-modal-stack' ).css( 'z-index', 1039 + (10 * $('body').data( 'fv_open_modals' )));
            $( '.modal-backdrop' ).not( 'fv-modal-stack' ).addClass( 'fv-modal-stack' ); 
         });
    });
</script>

<a data-toggle="modal" href="#myModal" class="btn btn-primary">Launch modal</a>

<div class="modal" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Modal 1</h4>
            </div>
            <div class="container"></div>
            <div class="modal-body">
               Content for the dialog / modal goes here.
               <br>
               <br>
               <br>
               <p>more content</p>
               <br>
               <br>
               <br>
               <a data-toggle="modal" href="#myModal2" class="btn btn-primary">Launch modal</a>
            </div>
        <div class="modal-footer">
          <a href="#" data-dismiss="modal" class="btn">Close</a>
          <a href="#" class="btn btn-primary">Save changes</a>
        </div>
      </div>
    </div>
</div>

<div class="modal" id="myModal2">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">Modal 2</h4>
        </div><div class="container"></div>
        <div class="modal-body">
          Content for the dialog / modal goes here.
          <br>
          <br>
          <p>come content</p>
          <br>
          <br>
          <br>
          <a data-toggle="modal" href="#myModal3" class="btn btn-primary">Launch modal</a>
        </div>
        <div class="modal-footer">
          <a href="#" data-dismiss="modal" class="btn">Close</a>
          <a href="#" class="btn btn-primary">Save changes</a>
        </div>
      </div>
    </div>
</div>

【讨论】:

    猜你喜欢
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2012-08-22
    • 2017-08-24
    相关资源
    最近更新 更多