溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么將Bootstrap模態(tài)垂直居中對(duì)齊

發(fā)布時(shí)間:2022-02-25 11:06:31 來(lái)源:億速云 閱讀:441 作者:小新 欄目:web開(kāi)發(fā)

小編給大家分享一下怎么將Bootstrap模態(tài)垂直居中對(duì)齊,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

答案:使用CSSmargin-top屬性

默認(rèn)情況下,Bootstrap模態(tài)窗口與頁(yè)面頂部對(duì)齊,并留有一些空白。但是您可以使用一個(gè)簡(jiǎn)單的JavaScript技巧將其垂直對(duì)齊在頁(yè)面中間,如下面的示例所述。此解決方案將動(dòng)態(tài)調(diào)整模式的對(duì)齊方式,即使用戶(hù)調(diào)整瀏覽器窗口的大小,也始終將其保持在頁(yè)面中央。

例試試這個(gè)代碼»

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Vertical Center Alignment of Bootstrap Modal Dialog</title>

<link rel="stylesheet" href="css/bootstrap.min.css">

<script src="js/jquery-3.5.1.min.js"></script>

<script src="js/bootstrap.min.js"></script>

<script>

$(document).ready(function(){

    function alignModal(){

        var modalDialog = $(this).find(".modal-dialog");

        // Applying the top margin on modal to align it vertically center

        modalDialog.css("margin-top", Math.max(0, ($(window).height() - modalDialog.height()) / 2));

    }

    // Align modal when it is displayed

    $(".modal").on("shown.bs.modal", alignModal);

    // Align modal when user resize the window

    $(window).on("resize", function(){

        $(".modal:visible").each(alignModal);

    });   

});

</script>

</head>

<body>

<div class="m-4">

    <!-- Button HTML (to Trigger Modal) -->

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

    <!-- Modal HTML -->

    <div id="myModal" class="modal">

        <div class="modal-dialog">

            <div class="modal-content">

                <div class="modal-header">

                    <h6 class="modal-title">Confirmation</h6>

                    <button type="button" class="close" data-dismiss="modal">&times;</button> 

                </div>

                <div class="modal-body">

                    <p>Do you want to save changes you made to document before closing?</p>

                    <p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>

                </div>

                <div class="modal-footer">

                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>

                    <button type="button" class="btn btn-primary">Save changes</button>

                </div>

            </div>

        </div>

    </div>

</div>     

</body>

</html>

以上是“怎么將Bootstrap模態(tài)垂直居中對(duì)齊”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI