Bootstrap 3 - set height of modal window according to screen size

前端 未结 6 487
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 03:58

I am trying to create a kind of responsive megamenu using Bootstrap 3 modal dialog. I would like to set the width and height of the whole modal window to 80% of the viewport

相关标签:
6条回答
  • 2020-12-13 04:05

    I am using jquery for this. I mad a function to set desired height to the modal(You can change that according to your requirement). Then I used Modal Shown event to call this function. Remember not to use $("#modal").show() rather use $("#modal").modal('show') otherwise shown.bs.modal will not be fired.

    That all I have for this scenario.

    var offset=250; //You can set offset accordingly based on your UI
    function AdjustPopup() 
    {
        $(".modal-body").css("height","auto");
        if ($(".modal-body:visible").height() > ($(window).height() - offset)) 
        {
            $(".modal-body:visible").css("height", ($(window).height() - offset));
        }
    }
    //Execute the function on every trigger on show() event.
    $(document).ready(function(){
    $('.modal').on('shown.bs.modal', function (e) {
            AdjustPopup();
        });
    });
    //Remember to show modal like this
    $("#MyModal").modal('show');

    0 讨论(0)
  • 2020-12-13 04:06

    To expand on Ryand's answer, if you're using Bootstrap.ui, this on your modal-instance will do the trick:

        modalInstance.rendered.then(function (result) {
            $('.modal .modal-body').css('overflow-y', 'auto'); 
            $('.modal .modal-body').css('max-height', $(window).height() * 0.7);
            $('.modal .modal-body').css('height', $(window).height() * 0.7);
        });
    
    0 讨论(0)
  • 2020-12-13 04:08

    Try:

    $('#myModal').on('show.bs.modal', function () {
    $('.modal-content').css('height',$( window ).height()*0.8);
    });
    
    0 讨论(0)
  • 2020-12-13 04:18

    Pure CSS solution, using calc

    .modal-body {
        max-height: calc(100vh - 200px);
        overflow-y: auto;
    }
    

    200px may be adjusted in accordance to height of header & footer

    0 讨论(0)
  • 2020-12-13 04:20

    I assume you want to make modal use as much screen space as possible on phones. I've made a plugin to fix this UX problem of Bootstrap modals on mobile phones, you can check it out here - https://github.com/keaukraine/bootstrap-fs-modal

    All you will need to do is to apply modal-fullscreen class and it will act similar to native screens of iOS/Android.

    0 讨论(0)
  • 2020-12-13 04:23

    Similar to Bass, I had to also set the overflow-y. That could actually be done in the CSS

    $('#myModal').on('show.bs.modal', function () {
        $('.modal .modal-body').css('overflow-y', 'auto'); 
        $('.modal .modal-body').css('max-height', $(window).height() * 0.7);
    });
    
    0 讨论(0)
提交回复
热议问题