How to scroll the page when a modal dialog is longer than the screen?

前端 未结 11 494
时光取名叫无心
时光取名叫无心 2020-12-22 20:42

I have a modal dialog in my app which can get quite long in the y direction. This introduces a problem whereby some of the content of the dialog is hidden off the bottom of

相关标签:
11条回答
  • 2020-12-22 21:15

    This is what fixed it for me:

    max-height: 100%;
    overflow-y: auto;
    

    EDIT: I now use the same method currently used on Twitter where the modal acts sort of like a separate page on top of the current content and the content behind the modal does not move as you scroll.

    In essence it is this:

    var scrollBarWidth = window.innerWidth - document.body.offsetWidth;
    $('body').css({
      marginRight: scrollBarWidth,
      overflow: 'hidden'
    });
    $modal.show();
    

    With this CSS on the modal:

    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    

    JSFiddle: https://jsfiddle.net/0x0049/koodkcng/
    Pure JS version (IE9+): https://jsfiddle.net/0x0049/koodkcng/1/

    This works no matter the height or width of the page or modal dialog, allows scrolling no matter where your mouse/finger is, doesn't have the jarring jump some solutions have that disable scroll on the main content, and looks great too.

    0 讨论(0)
  • 2020-12-22 21:16

    Change position

    position:fixed;
    overflow: hidden;
    

    to

    position:absolute;
    overflow:scroll;
    
    0 讨论(0)
  • 2020-12-22 21:20

    Here is my demo of modal window that auto-resize to its content and starts scrolling when it does not fit the window.

    Modal window demo (see comments in the HTML source code)

    All done only with HTML and CSS - no JS required to display and resize the modal window (but you still need it to display the window of course - in new version you don't need JS at all).

    Update (more demos):

    • Modal window aligned to top

    • Centered Modal window

    • Old demo that use Javascript

    The point is to have outer and inner DIVs where the outer one defines the fixed position and the inner creates the scrolling. (In the demo there are actually more DIVs to make them look like an actual modal window.)

            #modal {
                position: fixed;
                transform: translate(0,0);
                width: auto; left: 0; right: 0;
                height: auto; top: 0; bottom: 0;
                z-index: 990; /* display above everything else */
                padding: 20px; /* create padding for inner window - page under modal window will be still visible */
            }
    
            #modal .outer {
                box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; -o-box-sizing: border-box;
                width: 100%;
                height: 100%;
                position: relative;
                z-index: 999;
            }
    
            #modal .inner {
                box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; -o-box-sizing: border-box;
                width: 100%;
                height: auto;       /* allow to fit content (if smaller)... */
                max-height: 100%;   /* ... but make sure it does not overflow browser window */
    
                /* allow vertical scrolling if required */
                overflow-x: hidden;
                overflow-y: auto;
    
                /* definition of modal window layout */
                background: #ffffff;
                border: 2px solid #222222;
                border-radius: 16px; /* some nice (modern) round corners */
                padding: 16px;       /* make sure inner elements does not overflow round corners */
            }
    
    0 讨论(0)
  • 2020-12-22 21:21

    simple way you can do this by adding this css So, you just added this to CSS:

    .modal-body {
    position: relative;
    padding: 20px;
    height: 200px;
    overflow-y: scroll;
    }
    

    and it's working!

    0 讨论(0)
  • 2020-12-22 21:21

    position:fixed implies that, well, the modal window will remain fixed relative to the viewpoint. I agree with your assessment that it's appropriate in this scenario, with that in mind why don'y you add a scrollbar to the modal window itself?

    If so, correct max-height and overflow properties should do the trick.

    0 讨论(0)
提交回复
热议问题