簡體   English   中英

根據窗口高度引導模態高度

[英]Bootstrap modal height as per window height

我試圖使模態高度與窗口高度相同。 模態的高度是動態的,可以適當地擴展以適應內容,最多可達窗口高度的 90%。 還想在屏幕頂部留出一些空間來顯示標題。

這是我寫的:

 $(document).ready(function(){
    $('.modal').on('shown.bs.modal', function (e) {
        var modalObj = $(this).find('.modal-content');                                        
        if ($(modalObj).height() > ($(window).height()*0.8)) {
            $(modalObj).height($(window).height()*0.8);
        }                                         
    });
 })

問題是當我將設備從縱向切換到橫向或反之亦然時,第一次嘗試模式時會獲得前一模式的高度。

假設我在肖像模式下打開模態,它顯示 430 像素的高度。 比我關閉模態我將設備切換到橫向並打開模態,它顯示 430px(在縱向模式下的高度)但是如果我再次打開模態,它會得到正確的高度。

我認為當我關閉模態時,高度沒有被移除。 每次關閉模態或調整窗口大小時,我都必須寫一些清除高度的內容。

還試過:

var callback = function () {

  $('.modal').on('shown.bs.modal', function (e) {
        var modalObj = $(this).find('.modal-content');                                      
        if ($(modalObj).height() > ($(window).height()*0.9)) {
            $(modalObj).height($(window).height()*0.9);
        }                                         
    });
};

$(document).ready(callback);
$(window).resize(callback);

如果您希望模態占用視口高度的 90%,則不需要任何 JS。 CSS 有一個特殊的單位,稱為vh 100vh是視口的 100%。

在您的情況下,您只需要:

.modal {
  height: 90vh;
}

如有必要,您也可以將其更改為max-height

您的回調函數更改了shown.bs.modal事件的處理程序。 如果您的模態已經打開,則此處理程序已被執行,並且不會在窗口調整大小時再次觸發。

您需要為window.resize事件使用不同的處理程序。

// This will detect and set the height properly when the modal opens
$('.modal').on('shown.bs.modal', function (e) {
    var modalObj = $(this).find('.modal-content');
    $(modalObj).height('auto');
    if ($(modalObj).height() > ($(window).height() * 0.9)) {
        $(modalObj).height($(window).height() * 0.9);
    }
});

// This will detect and set the height properly when the window resizes
var callback = function () {
    jQuery('.modal').each(function (idx, item) {
        var modalObj = $(item).find('.modal-content');
        $(modalObj).height('auto');
        if ($(modalObj).height() > ($(window).height() * 0.9)) {
            $(modalObj).height($(window).height() * 0.9);
        }
    });
};

// Binding the callback in document.ready is not required, just on window.resize
$(window).resize(callback);

是 Bootply 中的演示。

實際上我們需要使用 height 到.modal-body以及overflow-y: auto屬性,而不是.modal 但首先我們需要為.modal-header.modal-footer留出一些空間。 所以,我將 75% 用於.modal-body ,25% 用於.modal-header.modal-footer 您可以根據自己的需要進行調整

.modal-body {
  max-height: 75vh;
  overflow-y: auto;
}

或者,如果您想以像素為單位進行調整

.modal-body {
   max-height: calc(100vh - 180px); // to adjust you can change 180px
   overflow-y: auto;
   }

調整<div> Bootstrap 網站上的 -height 到 window-height</div><div id="text_translate"><p> 假設我有一個使用<strong>Bootstrap 4</strong>制作的簡單網站,其中包含 header、導航欄、內容區域和頁腳。<br> 我的演示網站可以在 JSFiddle 上找到並實時編輯: <a href="https://jsfiddle.net/26zda5xv/2/" rel="nofollow noreferrer">https://jsfiddle.net/26zda5xv/2/</a><br> 請在下面找到該網站的屏幕截圖:</p><p> <a href="https://i.stack.imgur.com/aMvH9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aMvH9.png" alt="演示網站截圖"></a></p><p> 我希望&lt;div class="container"&gt;至少填寫整個頁面/窗口高度。 ( min-height? )<br> 這應該通過擴展&lt;div id="content"&gt;的高度來完成。</p><p> 當然,該解決方案應該自動處理各種屏幕分辨率/設備。</p><p> 如何根據 JSFiddle 正確執行此操作?<br> Bootstrap 4 中是否有針對此任務的內置解決方案?</p><p> 謝謝!</p></div>

[英]Adjust <div>-height to window-height on Bootstrap website

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 在窗口高度內拖動引導模態 角度UI-相對於窗口高度的模態高度 更改大型Bootstrap模式的高度 Angular 指令根據 window 高度動態調整組件高度 Firefox和Chrome中的Bootstrap模態輪播高度問題 引導模態元素的高度問題 Bootstrap模態體最大高度100% 動態創建的Bootstrap Modal高度錯誤 調整<div> Bootstrap 網站上的 -height 到 window-height</div><div id="text_translate"><p> 假設我有一個使用<strong>Bootstrap 4</strong>制作的簡單網站,其中包含 header、導航欄、內容區域和頁腳。<br> 我的演示網站可以在 JSFiddle 上找到並實時編輯: <a href="https://jsfiddle.net/26zda5xv/2/" rel="nofollow noreferrer">https://jsfiddle.net/26zda5xv/2/</a><br> 請在下面找到該網站的屏幕截圖:</p><p> <a href="https://i.stack.imgur.com/aMvH9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aMvH9.png" alt="演示網站截圖"></a></p><p> 我希望&lt;div class="container"&gt;至少填寫整個頁面/窗口高度。 ( min-height? )<br> 這應該通過擴展&lt;div id="content"&gt;的高度來完成。</p><p> 當然,該解決方案應該自動處理各種屏幕分辨率/設備。</p><p> 如何根據 JSFiddle 正確執行此操作?<br> Bootstrap 4 中是否有針對此任務的內置解決方案?</p><p> 謝謝!</p></div> Bootstrap 3 - 模態背景不會根據模態對話框的高度調整大小?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM