aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/overscroll-behavior/index.html
blob: 728f41aa9481d14275f4ee66702ef4bf3c87cda3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---
title: overscroll-behavior
slug: Web/CSS/overscroll-behavior
translation_of: Web/CSS/overscroll-behavior
---
<div>{{CSSRef}}</div>

<p><span class="seoSummary"><strong><code>overscroll-behavior</code></strong> CSS 属性是 {{cssxref("overscroll-behavior-x")}}{{cssxref("overscroll-behavior-y")}} 属性的合并写法, 让你可以控制浏览器过度滚动时的表现——也就是滚动到边界。</span></p>

<pre class="brush:css no-line-numbers">/* 关键字的值 */
overscroll-behavior: auto; /* 默认 */
overscroll-behavior: contain;
overscroll-behavior: none;

/* 使用2个值 */
overscroll-behavior: auto contain;

/* Global values */
overflow: inherit;
overflow: initial;
overflow: unset;
</pre>

<p>默认情况下,当触及页面顶部或者底部时(或者是其他可滚动区域),移动端浏览器倾向于提供一种“触底”效果,甚至进行页面刷新。你可能也发现了,当对话框中含有可滚动内容时,一旦滚动至对话框的边界,对话框下方的页面内容也开始滚动了——这被称为“滚动链”。 </p>

<p>在某些情况下我们不想要这些表现,使用 <code>overscroll-behavior</code> 来去除不需要的滚动链,以及类似 QQ 一类的应用下拉刷新效果。</p>

<p>{{cssinfo}}</p>

<h2 id="语法">语法</h2>

<p><code>overscroll-behavior</code> 属性可以使用下面列表中的一或两个关键字指定。</p>

<p>使用两个关键字来指定 <code>overscroll-behavior</code> 分别在 <code>x</code> 和 <code>y</code> 轴的值。只用一个值的话,x 和 y 轴都被指定为该值。</p>

<h3 id="值"></h3>

<dl>
 <dt><code>auto</code></dt>
 <dd>默认效果</dd>
 <dt><code>contain</code></dt>
 <dd>设置这个值后,默认的滚动边界行为不变(“触底”效果或者刷新),但是临近的滚动区域不会被滚动链影响到,比如对话框后方的页面不会滚动。</dd>
 <dt><code>none</code></dt>
 <dd>临近滚动区域不受到滚动链影响,而且默认的滚动到边界的表现也被阻止。</dd>
</dl>

<h3 id="Formal_syntax">Formal syntax</h3>

<pre class="syntaxbox">{{csssyntax}}</pre>

<h2 id="Examples">Examples</h2>

<p>在我们 <a href="https://mdn.github.io/css-examples/overscroll-behavior/">滚动行为示例</a> (也可以看该 <a href="https://github.com/mdn/css-examples/tree/master/overscroll-behavior">源代码</a>)我们展现一个虚构的联系人全页列表,和一个包含聊天窗口的对话框。</p>

<p><img alt="" src="https://mdn.mozillademos.org/files/15778/example.png" style="border-style: solid; border-width: 1px; display: block; height: 622px; margin: 0px auto; width: 350px;"></p>

<p>正常情况下在聊天窗口边界之前都可以使用滚动条,但是当滚动到边界之后,其后的联系人窗口也将开始滚动,这并不是我们满意的效果。可以在聊天窗口中使用 <code>overscroll-behavior-y</code> (<code>overscroll-behavior</code> 也可以) 就像如下:</p>

<pre class="brush: css">.messages {
  height: 220px;
  overflow: auto;
  overscroll-behavior-y: contain;
} </pre>

<p>我们也希望移除标准的滚动至顶部或底部的滚动特效(例如Android上的Chrome当滚动超过顶部边界时会刷新页面),可以通过在 {{htmlelement("body")}} 元素设置 <code>overscroll-behavior: none</code> 来阻止这个行为:</p>

<pre class="brush: css">body {
  margin: 0;
  overscroll-behavior: none;
}</pre>

<h2 id="Specifications">Specifications</h2>

<p>在 CSSWG 发布自己的草案之前,该规范只能在它的 <a href="https://wicg.github.io/overscroll-behavior/">WICG Github Repository</a> 中找到。</p>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('Overscroll Behavior', '#propdef-overscroll-behavior', 'overscroll-behavior')}}</td>
   <td>{{Spec2('Overscroll Behavior')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("css.properties.overscroll-behavior")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="https://developers.google.com/web/updates/2017/11/overscroll-behavior#demo">Take control of your scroll: customizing pull-to-refresh and overflow effects</a></li>
 <li>{{cssxref("-ms-scroll-chaining")}}</li>
</ul>





<p>
 <audio class="hidden"></audio>
</p>