当模态对话框比屏幕长时,如何滚动页面?

我有一个模态对话框在我的应用程序,可以得到相当长的 y 方向。这引入了一个问题,即对话框的一些内容隐藏在页面的底部。

enter image description here

我希望窗口滚动条滚动对话框显示时,它太长适合在屏幕上,但留下后面的模态主体的地方。如果你使用 特雷洛,那么你就知道我要用什么了。

如果不使用 JavaScript 控制滚动条,这可能吗?

下面是我到目前为止应用到我的模态和对话框的 CSS:

body.blocked {
overflow: hidden;
}


.modal-screen {
background: #717174;
position: fixed;
overflow: hidden;
width: 100%;
height: 100%;
top: 0;
left: 0;
opacity: 0.9;
z-index: 50;
}


.dialog {
background: #fff;
position: fixed;
padding: 12px;
top: 20%;
left: 50%;
z-index: 10000;
border-radius: 5px;
box-shadow: 0, 0, 8px, #111;
}
232829 次浏览

Change position

position:fixed;
overflow: hidden;

to

position:absolute;
overflow:scroll;

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.

In the end I had had to make changes to the content of the page behind the modal screen to ensure that it never got long enough to scroll the page.

Once I did that, the problems I encountered when applying position: absolute to the dialog were resolved as the user could no longer scroll down the page.

fixed positioning alone should have fixed that problem but another good workaround to avoid this issue is to place your modal divs or elements at the bottom of the page not within your sites layout. Most modal plugins give their modal positioning absolute to allow the user keep main page scrolling.

<html>
<body>
<!-- Put all your page layouts and elements




<!-- Let the last element be the modal elemment  -->
<div id="myModals">
...
</div>


</body>
</html>

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.

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):

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 */
}

Window Page Scrollbar clickable when Modal is open

This one works for me. Pure CSS.

<style type="text/css">


body.modal-open {
padding-right: 17px !important;
}


.modal-backdrop.in {
margin-right: 16px;


</style>

Try it and let me know

Try:

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

It will arrange your modal and then give it an vertical scroll

Here.. Works perfectly for me

.modal-body {
max-height:500px;
overflow-y:auto;
}

I wanted to add my pure CSS answer to this problem of modals with dynamic width and height. The following code also works with the following requirements:

  1. Place modal in center of screen
  2. If modal is higher than viewport, scroll dimmer (not modal content)

HTML:

<div class="modal">
<div class="modal__content">
(Long) Content
</div>
</div>

CSS/LESS:

.modal {
position: fixed;
display: flex;
align-items: center;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding: @qquad;
overflow-y: auto;
background: rgba(0, 0, 0, 0.7);
z-index: @zindex-modal;


&__content {
width: 900px;
margin: auto;
max-width: 90%;
padding: @quad;
background: white;
box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.75);
}
}

This way the modal is always within the viewport. The width and height of the modal are as flexible as you like. I removed my close icon from this for simplicity.

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!