试一试
overscroll-behavior: auto;
overscroll-behavior: contain;
overscroll-behavior: none;
<section class="default-example" id="default-example">
<div class="example-container">
<div class="box">
This is a scrollable container. Michaelmas term lately over, and the Lord
Chancellor sitting in Lincoln's Inn Hall. Implacable November weather. As
much mud in the streets as if the waters had but newly retired from the
face of the earth.
<br /><br />
Lorem Ipsum has been the industry's standard dummy text ever since the
1500s, when an unknown printer took a galley of type and scrambled it to
make a type specimen book. It has survived not only five centuries, but
also the leap into electronic typesetting, remaining essentially
unchanged.
</div>
<div id="example-element">
This is the inner container. Focus on this container, scroll to the bottom
and when you reach the bottom keep scrolling.
<p>
If you have
<code class="language-css">overscroll-behavior: auto;</code> selected
the outer container will start to scroll.
</p>
If you have
<code class="language-css">overscroll-behavior: contain;</code> selected,
the outer container will not scroll unless you move your cursor out of the
inner container and try to perform scroll on the outer container.
</div>
</div>
</section>
.example-container {
width: 35em;
height: 18em;
border: medium dotted;
padding: 0.75em;
text-align: left;
overflow: auto;
display: flex;
}
.box {
width: 50%;
}
#example-element {
width: 50%;
height: 12em;
border: medium dotted #1b76c4;
padding: 0.3em;
margin: 0 0.3em;
text-align: left;
overflow: auto;
overscroll-behavior: contain;
}
构成属性
此属性是以下 CSS 属性的简写:
语法
/* Keyword values */
overscroll-behavior: auto; /* default */
overscroll-behavior: contain;
overscroll-behavior: none;
/* Two values */
overscroll-behavior: auto contain;
/* Global values */
overscroll-behavior: inherit;
overscroll-behavior: initial;
overscroll-behavior: revert;
overscroll-behavior: revert-layer;
overscroll-behavior: unset;
overscroll-behavior 属性被指定为一个或两个关键字,从下面的值列表中选择。
两个关键字分别指定 x 和 y 轴上的 overscroll-behavior 值。如果只指定一个值,则 x 和 y 被假定为具有相同的值。
值
描述
默认情况下,移动浏览器在页面(或其他滚动区域)的顶部或底部达到时,往往会提供“弹跳”效果,甚至页面刷新。你可能还注意到,当你有一个带有可滚动内容的对话框位于一个也带有可滚动内容的页面顶部时,一旦对话框的滚动边界达到,下层页面就会开始滚动——这被称为滚动链接。
在某些情况下,这些行为是不希望的。你可以使用 overscroll-behavior 来摆脱不需要的滚动链接和浏览器受 Facebook/Twitter 应用启发的“下拉刷新”类型的行为。
请注意,此属性仅适用于滚动容器。特别是,由于 不是滚动容器,在此 iframe 上设置此属性无效。要控制来自 iframe 的滚动链接,请在 iframe 文档的 和 元素上设置 overscroll-behavior。
正式定义
正式语法
overscroll-behavior =
[ contain | none | auto ]{1,2}
示例
防止下层元素滚动
在我们的 overscroll-behavior 示例(也请参阅源代码)中,我们展示了一个全页的虚假联系人列表和一个包含聊天窗口的对话框。

这两个区域都可滚动;通常,如果你滚动聊天窗口直到达到滚动边界,下层的联系人窗口也会开始滚动,这是不希望的。这可以通过在聊天窗口上使用 overscroll-behavior-y(overscroll-behavior 也可以)来阻止,如下所示
.messages {
height: 220px;
overflow: auto;
overscroll-behavior-y: contain;
}
我们还希望消除联系人滚动到顶部或底部时的标准过度滚动效果(例如,Android 上的 Chrome 在滚动超出顶部边界时会刷新页面)。这可以通过在元素上设置 overscroll-behavior: none 来阻止。
html {
margin: 0;
overscroll-behavior: none;
}
规范
| 规范 |
|---|
| CSS Overscroll Behavior Module Level 1 # overscroll-behavior 属性 |
浏览器兼容性
加载中…
另见
- CSS overscroll behavior 模块
- CSS 滚动锚定模块
- 掌控你的滚动:自定义下拉刷新和溢出效果,在 developer.chrome.com 上 (2017)