Twitter bootstrap scrollable modal

前端 未结 10 1038
日久生厌
日久生厌 2020-12-22 19:03

I\'m using twitter bootstrap for a project I am working on.

I have a modal window that has a somewhat longer form in it and I didn\'t like that when the window got

相关标签:
10条回答
  • 2020-12-22 19:44

    I also added

    .modal { position: absolute; }
    

    to the stylesheet to allow the dialog to scroll, but if the user has moved down to the bottom of a long page the modal can end up hidden off the top of the visible area.

    I understand this is no longer an issue in bootstrap 3, but looking for a relatively quick fix until we upgrade I ended up with the above plus calling the following before opening the modal

    $('.modal').css('top', $(document).scrollTop() + 50);
    

    Seems to be happy in FireFox, Chrome, IE10 8 & 7 (the browsers I had to hand)

    0 讨论(0)
  • 2020-12-22 19:44

    None of this will work as expected.. The correct way is to change position: fixed to position: absolute for .modal class

    0 讨论(0)
  • 2020-12-22 19:45
    /* Important part */
    .modal-dialog{
        overflow-y: initial !important
    }
    .modal-body{
        max-height: calc(100vh - 200px);
        overflow-y: auto;
    }
    

    This works for Bootstrap 3 without JS code and is responsive.

    Source

    0 讨论(0)
  • 2020-12-22 19:57

    In case of using .modal {overflow-y: auto;}, would create additional scroll bar on the right of the page, when body is already overflowed.

    The work around for this is to remove overflow from the body tag temporarily and set modal overflow-y to auto.

    Example:

    $('.myModalSelector').on('show.bs.modal', function () {
    
            // Store initial body overflow value
            _initial_body_overflow = $('body').css('overflow');
    
            // Let modal be scrollable
            $(this).css('overflow-y', 'auto');
            $('body').css('overflow', 'hidden');
    
    
        }).on('hide.bs.modal', function () {
    
            // Reverse previous initialization
            $(this).css('overflow-y', 'hidden');
            $('body').css('overflow', _initial_body_overflow);
    });
    
    0 讨论(0)
提交回复
热议问题