overscroll-behavior

Baseline 已广泛支持

此功能已成熟,并可在多种设备和浏览器版本上运行。自 2022 年 9 月起,所有浏览器都已支持此功能。

overscroll-behavior CSS 属性设置浏览器在滚动区域边界处执行的操作。

试一试

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 属性的简写:

语法

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 属性被指定为一个或两个关键字,从下面的值列表中选择。

两个关键字分别指定 xy 轴上的 overscroll-behavior 值。如果只指定一个值,则 x 和 y 被假定为具有相同的值。

auto

默认的滚动溢出行为正常发生。

contain

默认的滚动溢出行为(例如,“弹跳”效果)在设置此值的元素内部可见。但是,邻近的滚动区域不会发生滚动链接;下层元素不会滚动。contain 值会禁用原生浏览器导航,包括垂直的下拉刷新手势和水平的滑动导航。

none

邻近的滚动区域不会发生滚动链接,并且会阻止默认的滚动溢出行为。

描述

默认情况下,移动浏览器在页面(或其他滚动区域)的顶部或底部达到时,往往会提供“弹跳”效果,甚至页面刷新。你可能还注意到,当你有一个带有可滚动内容的对话框位于一个也带有可滚动内容的页面顶部时,一旦对话框的滚动边界达到,下层页面就会开始滚动——这被称为滚动链接

在某些情况下,这些行为是不希望的。你可以使用 overscroll-behavior 来摆脱不需要的滚动链接和浏览器受 Facebook/Twitter 应用启发的“下拉刷新”类型的行为。

请注意,此属性仅适用于滚动容器。特别是,由于