Bootstrap multiple modals modal-backdrop issue

前端 未结 5 1956
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 07:09

I have a page where one Bootstrap modal opens another modal.

The problem is that with each opened modal, it adds

相关标签:
5条回答
  • 2021-01-02 07:34

    Here is the working demo that I think will fit in your case.

    $(".modal").on("shown.bs.modal", function () {
        if ($(".modal-backdrop").length > 1) {
            $(".modal-backdrop").not(':first').remove();
        }
    })
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
     <div class="container">
    
            <!-- Trigger the modal with a button -->
            <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
    
            <!-- Modal -->
            <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog">
    
                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-body">
                            <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal2">Open Modal</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Modal -->
            <div class="modal fade" id="myModal2" role="dialog">
                <div class="modal-dialog">
    
                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-body">
                            <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal3">Open Modal 3</button>
                        </div>
                    </div>
                </div>
    
            </div>
        </div>

    0 讨论(0)
  • 2021-01-02 07:41

    // Make sure only one backdrop is rendered 
     $(document).on('show.bs.modal', '.modal', function () {
            if ($(".modal-backdrop").length > 1) {
                $(".modal-backdrop").not(':first').remove();
            }
        });
        // Remove all backdrop on close
        $(document).on('hide.bs.modal', '.modal', function () {
            if ($(".modal-backdrop").length > 1) {
                $(".modal-backdrop").remove();
            }
        });

    0 讨论(0)
  • 2021-01-02 07:43
    $(document).on('show.bs.modal', '.modal', function () {
        if ($(".modal-backdrop").length > -1) {
            $(".modal-backdrop").not(':first').remove();
        }
    });
    
    0 讨论(0)
  • 2021-01-02 08:00

    Let CSS handle it.

    .modal-backdrop:nth-child(2n-1) {
      opacity : 0;
    }
    

    https://jsfiddle.net/zsk4econ/3/

    0 讨论(0)
  • 2021-01-02 08:00

    You can add/remove data-backdrop="false" attribute depends on requirement. Else you can also include css like

    .modal-backdrop+.modal-backdrop {
      opacity : 0;
    }
    
    0 讨论(0)
提交回复
热议问题