如何使内部div使用窗口滚动条?

时间:2022-12-05 23:18:38

I'd like to use a div within my page that does not take up the whole screen use the scroll bars on the edge of the screen when its content overflows. I know that sounds confusing, so here's an example: http://www.csszengarden.com/?cssfile=/202/202.css&page=1

我想在我的页面中使用一个不占用整个屏幕的div,当它的内容溢出时使用屏幕边缘的滚动条。我知道这听起来令人困惑,所以这里有一个例子:http://www.csszengarden.com/?cssfile = 20/20 / 2.css&page = 1

I would have posted code, but I'm not sure where to start on that. Could someone point me in the right direction with the HTML & CSS?

我会发布代码,但我不知道从哪里开始。有人可以用HTML和CSS指出我正确的方向吗?

2 个解决方案

#1


22  

One trick is to overlay one or more static images or boxes and fix their position, then resize the scrollable area of your entire page, your inner <div>, such that its width fits in the "viewable area" exposed (i.e. not covered) by your overlaid area (you could use padding to achieve this).

一个技巧是覆盖一个或多个静态图像或框并修复它们的位置,然后调整整个页面的可滚动区域,内部

,使其宽度适合暴露在“可视区域”(即未覆盖)通过覆盖区域(您可以使用填充来实现此目的)。

I've made a demo page to illustrate this. It does involve extra markup per my suggestion though; I'd wager that more sophisticated CSS is used to achieve the same effect with the same base markup provided by CSS Zen Garden, but my answer is just another simpler way of going about it.

我做了一个演示页来说明这一点。根据我的建议,它确实涉及额外的标记;我打赌用CSS Zen Garden提供的相同基本标记来使用更复杂的CSS来实现相同的效果,但我的答案只是另一种更简单的方法。

The source code is provided below. The snippet is runnable but as the dimensions are unmodified from the original you will want to view it in full screen to see anything meaningful in action.

源代码如下。该代码段是可运行的,但由于尺寸未经原始版本修改,因此您需要全屏查看它以查看有效的内容。

body {
    margin: 0;
}

h1 {
    text-align: center;
}

#overlay div {
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: gray;
}

/* The top and bottom "borders". */
#overlay .vert {
    height: 150px;
}

/* The left and right "borders". */
#overlay .hor {
    width: 300px;
}

/* Anchor the parts of your overlay boxes to the respective edges of the page. */
#overlay-top { top: 0; }
#overlay-bottom { bottom: 0; }
#overlay-left { left: 0; }
#overlay-right { right: 0; }

/* Your content has padding equal to the sizes of your overlay "borders". */
#content {
    padding: 150px 300px;
}
<!-- This acts as the overlay, or mask, of your content. -->
<div id="overlay">
    <div id="overlay-top" class="vert"></div>
    <div id="overlay-bottom" class="vert"></div>
    <div id="overlay-left" class="hor"></div>
    <div id="overlay-right" class="hor"></div>
</div>

<!-- The entire content of your page would go here. -->
<div id="content">
    <h1>Top of content</h1>
    <hr>
    <p>This is a demonstration of the effect of scrolling a small content area in a page using the window's scrollbars.</p>
    <p>It was created as an illustration of <a href="http://*.com/a/5383466/106224">this CSS answer on Stack Overflow</a>.</p>
    <hr>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. No vix everti corpora corrumpit, et mea exerci ponderum eloquentiam, vis an idque nominati. Ea mel errem conclusionemque, cu sea dicat omnesque. Facilis detracto reprehendunt mei ei.</p>
    <p>Vim aperiam suscipit ne, et has euismod senserit, est ei ornatus mnesarchum. His delicata referrentur ad, vim an sapientem molestiae laboramus, ad quem clita consequuntur ius. Pro ei sint voluptatibus. Elaboraret definitionem per cu, liber deserunt interpretaris ad duo, cu vis petentium vulputate. His at vide patrioque, in labitur alterum sea.</p>
    <p>Putent mentitum referrentur no usu. Ne autem eripuit delectus mei, forensibus persequeris vim eu, sint quaestio ad nec. Offendit aliquyam ius ex. Ex cum option noluisse. Repudiare suscipiantur est ut.</p>
    <p>Ut vero scripta argumentum per, id nemore splendide appellantur vis. Et idque aliquam praesent nec. At wisi impetus deseruisse per, salutatus omittantur ex has. No sed accusata iudicabit, eu corrumpit deseruisse eos, et vis dictas suavitate. Ad quod abhorreant dissentias duo, ius ea quando eruditi nusquam.</p>
    <p>Eos autem debet ut, vel ea omnium dolorum eligendi. Ex has magna assum iudico. Eu dictas hendrerit vis, animal dissentiunt mel cu. Cu usu nemore interesset temporibus. Mea ut adipisci praesent consequuntur, per et brute legimus commune. Pri eirmod dissentiunt ei.</p>
    <p>In nonumy nostro invenire cum. Postea complectitur sit id. An saepe animal omittam usu, quem tantas id est, nam sint deleniti in. Dicit viderer reformidans ut quo. Mei ornatus molestie ut, id dolores vivendum menandri ius. Facer possim reformidans vix ut. Et utinam possim percipitur quo.</p>
    <p>Nisl perpetua voluptaria vim et, cu per unum efficiendi, no labore instructior pri. In usu delenit intellegam, at delicata praesent eos. Cu civibus mentitum duo. Eu movet dolor vim, has cu senserit referrentur. An mel corpora comprehensam, constituto vituperata cum eu.</p>
    <p>Ne vim nemore vidisse rationibus. Id partem percipit quaerendum mel. Soleat graecis placerat cu eum, sed at dolores recteque. In sit consul appetere, cum exerci legere accommodare ad. Cum at legimus patrioque neglegentur.</p>
    <p>No alii aeque facilisis mea, his novum dissentias id. Ne his exerci feugait, mei te suas graeci iracundia. Ad paulo soluta vim, discere qualisque his eu. Ius ut fuisset offendit inciderint, mel putant impetus appellantur no. Sit ad integre officiis euripidis, libris necessitatibus at per. Tamquam insolens an duo, sed exerci electram ad, qui at doming inciderint adversarium.</p>
    <p>Cum ex ornatus accusam, vocent dolorem definiebas sit et, ut illud takimata mei. Eos ex sale brute, bonorum mediocrem an nam. Ei alienum quaestio maiestatis has, iriure vivendo cu est, pri harum periculis ex. Per ad idque populo omnesque, novum reformidans definitionem eu nec. Mel reque evertitur voluptatum ea, inani iuvaret sententiae et has.</p>
    <hr>
    <h1>Bottom of content</h1>
</div>

#2


0  

BoltClock answer is good when the surrounding of the div is fixed always, but when you want to use the scroll bars of the page when the div (like a modal dialog) appear on the fly it can't be used.

当div的周围环境始终固定时,BoltClock的答案是好的,但是当你想要使用div(如模态对话框)时,你想要使用页面的滚动条,它就无法使用。

For this situations you can disable and hide the page scroll bars by applying this style to the body:

对于这种情况,您可以通过将此样式应用于正文来禁用和隐藏页面滚动条:

body {overflow:hidden}

The given div must fill the whole page and have scroll bars:

给定的div必须填满整个页面并有滚动条:

div {position:fixed; top:0; left:0; width:100%; height:100%; overflow-y:scroll}

now the div scroll bars appears exactly where page scroll bar existed and it's content would be scrollable.

现在div滚动条正好出现在页面滚动条存在的位置,它的内容可以滚动。

when the div is closed the body overflow style must be reset.

当div关闭时,必须重置主体溢出样式。

look at this jsfiddle: http://jsfiddle.net/rahbari/kxoexa5s/

看看这个jsfiddle:http://jsfiddle.net/rahbari/kxoexa5s/

#1


22  

One trick is to overlay one or more static images or boxes and fix their position, then resize the scrollable area of your entire page, your inner <div>, such that its width fits in the "viewable area" exposed (i.e. not covered) by your overlaid area (you could use padding to achieve this).

一个技巧是覆盖一个或多个静态图像或框并修复它们的位置,然后调整整个页面的可滚动区域,内部

,使其宽度适合暴露在“可视区域”(即未覆盖)通过覆盖区域(您可以使用填充来实现此目的)。

I've made a demo page to illustrate this. It does involve extra markup per my suggestion though; I'd wager that more sophisticated CSS is used to achieve the same effect with the same base markup provided by CSS Zen Garden, but my answer is just another simpler way of going about it.

我做了一个演示页来说明这一点。根据我的建议,它确实涉及额外的标记;我打赌用CSS Zen Garden提供的相同基本标记来使用更复杂的CSS来实现相同的效果,但我的答案只是另一种更简单的方法。

The source code is provided below. The snippet is runnable but as the dimensions are unmodified from the original you will want to view it in full screen to see anything meaningful in action.

源代码如下。该代码段是可运行的,但由于尺寸未经原始版本修改,因此您需要全屏查看它以查看有效的内容。

body {
    margin: 0;
}

h1 {
    text-align: center;
}

#overlay div {
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: gray;
}

/* The top and bottom "borders". */
#overlay .vert {
    height: 150px;
}

/* The left and right "borders". */
#overlay .hor {
    width: 300px;
}

/* Anchor the parts of your overlay boxes to the respective edges of the page. */
#overlay-top { top: 0; }
#overlay-bottom { bottom: 0; }
#overlay-left { left: 0; }
#overlay-right { right: 0; }

/* Your content has padding equal to the sizes of your overlay "borders". */
#content {
    padding: 150px 300px;
}
<!-- This acts as the overlay, or mask, of your content. -->
<div id="overlay">
    <div id="overlay-top" class="vert"></div>
    <div id="overlay-bottom" class="vert"></div>
    <div id="overlay-left" class="hor"></div>
    <div id="overlay-right" class="hor"></div>
</div>

<!-- The entire content of your page would go here. -->
<div id="content">
    <h1>Top of content</h1>
    <hr>
    <p>This is a demonstration of the effect of scrolling a small content area in a page using the window's scrollbars.</p>
    <p>It was created as an illustration of <a href="http://*.com/a/5383466/106224">this CSS answer on Stack Overflow</a>.</p>
    <hr>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. No vix everti corpora corrumpit, et mea exerci ponderum eloquentiam, vis an idque nominati. Ea mel errem conclusionemque, cu sea dicat omnesque. Facilis detracto reprehendunt mei ei.</p>
    <p>Vim aperiam suscipit ne, et has euismod senserit, est ei ornatus mnesarchum. His delicata referrentur ad, vim an sapientem molestiae laboramus, ad quem clita consequuntur ius. Pro ei sint voluptatibus. Elaboraret definitionem per cu, liber deserunt interpretaris ad duo, cu vis petentium vulputate. His at vide patrioque, in labitur alterum sea.</p>
    <p>Putent mentitum referrentur no usu. Ne autem eripuit delectus mei, forensibus persequeris vim eu, sint quaestio ad nec. Offendit aliquyam ius ex. Ex cum option noluisse. Repudiare suscipiantur est ut.</p>
    <p>Ut vero scripta argumentum per, id nemore splendide appellantur vis. Et idque aliquam praesent nec. At wisi impetus deseruisse per, salutatus omittantur ex has. No sed accusata iudicabit, eu corrumpit deseruisse eos, et vis dictas suavitate. Ad quod abhorreant dissentias duo, ius ea quando eruditi nusquam.</p>
    <p>Eos autem debet ut, vel ea omnium dolorum eligendi. Ex has magna assum iudico. Eu dictas hendrerit vis, animal dissentiunt mel cu. Cu usu nemore interesset temporibus. Mea ut adipisci praesent consequuntur, per et brute legimus commune. Pri eirmod dissentiunt ei.</p>
    <p>In nonumy nostro invenire cum. Postea complectitur sit id. An saepe animal omittam usu, quem tantas id est, nam sint deleniti in. Dicit viderer reformidans ut quo. Mei ornatus molestie ut, id dolores vivendum menandri ius. Facer possim reformidans vix ut. Et utinam possim percipitur quo.</p>
    <p>Nisl perpetua voluptaria vim et, cu per unum efficiendi, no labore instructior pri. In usu delenit intellegam, at delicata praesent eos. Cu civibus mentitum duo. Eu movet dolor vim, has cu senserit referrentur. An mel corpora comprehensam, constituto vituperata cum eu.</p>
    <p>Ne vim nemore vidisse rationibus. Id partem percipit quaerendum mel. Soleat graecis placerat cu eum, sed at dolores recteque. In sit consul appetere, cum exerci legere accommodare ad. Cum at legimus patrioque neglegentur.</p>
    <p>No alii aeque facilisis mea, his novum dissentias id. Ne his exerci feugait, mei te suas graeci iracundia. Ad paulo soluta vim, discere qualisque his eu. Ius ut fuisset offendit inciderint, mel putant impetus appellantur no. Sit ad integre officiis euripidis, libris necessitatibus at per. Tamquam insolens an duo, sed exerci electram ad, qui at doming inciderint adversarium.</p>
    <p>Cum ex ornatus accusam, vocent dolorem definiebas sit et, ut illud takimata mei. Eos ex sale brute, bonorum mediocrem an nam. Ei alienum quaestio maiestatis has, iriure vivendo cu est, pri harum periculis ex. Per ad idque populo omnesque, novum reformidans definitionem eu nec. Mel reque evertitur voluptatum ea, inani iuvaret sententiae et has.</p>
    <hr>
    <h1>Bottom of content</h1>
</div>

#2


0  

BoltClock answer is good when the surrounding of the div is fixed always, but when you want to use the scroll bars of the page when the div (like a modal dialog) appear on the fly it can't be used.

当div的周围环境始终固定时,BoltClock的答案是好的,但是当你想要使用div(如模态对话框)时,你想要使用页面的滚动条,它就无法使用。

For this situations you can disable and hide the page scroll bars by applying this style to the body:

对于这种情况,您可以通过将此样式应用于正文来禁用和隐藏页面滚动条:

body {overflow:hidden}

The given div must fill the whole page and have scroll bars:

给定的div必须填满整个页面并有滚动条:

div {position:fixed; top:0; left:0; width:100%; height:100%; overflow-y:scroll}

now the div scroll bars appears exactly where page scroll bar existed and it's content would be scrollable.

现在div滚动条正好出现在页面滚动条存在的位置,它的内容可以滚动。

when the div is closed the body overflow style must be reset.

当div关闭时,必须重置主体溢出样式。

look at this jsfiddle: http://jsfiddle.net/rahbari/kxoexa5s/

看看这个jsfiddle:http://jsfiddle.net/rahbari/kxoexa5s/