如何在容器div中显示bootstrap v4模型框?

时间:2015-10-16 14:40:37

标签: javascript jquery css twitter-bootstrap bootstrap-4

使用bootstrap v4.0.0-alpha是非常棒的,这就是我要找的:我需要将模式框放在容器div中,而不是整个屏幕上。 enter image description here

我尝试更改一些内置的CSS类,是的,有些JavaScript :),如:

  

z-index:1051;

更改右侧navBar但不满足于那个,任何简单的解决方案?

1 个答案:

答案 0 :(得分:4)

  1. 创建变量var mod并在其中加载模态;

    var mod = $('.modal');
    
  2. 在容器内创建div <div class="insidemodal"></div>,以显示模态

  3. 使用Bootstrap 4 modal event listener,首选show.bs.modal并将var mod = $('.modal');放入其中,并在模拟即将显示时在insidemodal选择器中加载模式。

    $('#myModal').on('show.bs.modal', function () {
        var mod = $('.modal'); //Create variable and load modal in it
        $('.insidemodal').html(mod); //Load modal to `insidemodal` Selector
    });
    
  4. Modal CSS

    中进行以下更改
    .modal-backdrop {
        display:none //<---Kill the modal backdrop
    }
    .modal-backdrop.in {
        opacity: 0;
    }
    .modal {
        z-index: inherit !important; //<--overwrite modal default z-index: 1050
        position: relative; //<change position from absoulte
    }
    
  5. 所以最终守则将是

    JS

    $(document).ready(function () {
        $('#myModal').on('show.bs.modal', function () {
            var mod = $('.modal'); 
            $('.insidemodal').html(mod);
        });
    });
    

    CSS

    .title {
        background: green;
        color:#fff;
        padding: 5px;
        text-align:center;
        border:1px solid;
    }
    .menu {
        background: blue;
        color:#fff;
        padding: 5px;
        text-align:center;
        border:1px solid;
    }
    .insidemodal {
        background: red;
        border:1px solid;
        padding: 5px;
    }
    .modal-backdrop {
        display:none
    }
    .modal-backdrop.in {
        opacity: 0;
    }
    .modal {
        z-index: inherit !important;
        position: relative;
    }
    

    HTML

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="title">This Is Title</div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-4">
                <div class="menu">Left Menu
                    <br>
                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Modal</button>
                </div>
            </div>
            <div class="col-sm-8">
                <div class="insidemodal"></div>
            </div>
        </div>
    </div>
    <!-- Modal -->
    <div class="modal fade" id="myModal">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span>
     <span class="sr-only">Close</span>
    
                    </button>
                     <h4 class="modal-title" id="myModalLabel">Modal title</h4>
    
                </div>
                <div class="modal-body">...</div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal" aria-label="Close">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
    

    Working Fiddle Example

    SideNote 如果您有多个模态,Modal CSS中的上述CSS更改可能会导致其他模态无法正常运行,因此请使用自定义选择器而不是在默认模式选择器中进行更改。