aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/scrollintoview/index.html
blob: 3cba87dabfacdfa36987ca4394083abb7ffee504 (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
---
title: Element.scrollIntoView()
slug: Web/API/Element/scrollIntoView
translation_of: Web/API/Element/scrollIntoView
---
<div>{{APIRef("DOM")}}</div>

<div><span class="seoSummary">{{domxref("Element")}} </span>接口的scrollIntoView()方法会滚动元素的父容器,使被调用scrollIntoView()的元素对用户可见。</div>

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

<pre class="syntaxbox notranslate">element.scrollIntoView(); // 等同于element.scrollIntoView(true)
element.scrollIntoView(alignToTop); // Boolean型参数
element.scrollIntoView(scrollIntoViewOptions); // Object型参数</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>alignToTop</code>{{optional_inline}}</dt>
 <dd>一个{{jsxref("Boolean")}}值:
 <ul>
  <li>如果为<code>true</code>,元素的顶端将和其所在滚动区的可视区域的顶端对齐。相应的 <code>scrollIntoViewOptions: {block: "start", inline: "nearest"}</code>。这是这个参数的默认值。</li>
  <li>如果为<code>false</code>,元素的底端将和其所在滚动区的可视区域的底端对齐。相应的<code>scrollIntoViewOptions: {block: "end", inline: "nearest"}</code></li>
 </ul>
 </dd>
 <dt><code>scrollIntoViewOptions</code> {{optional_inline}} {{experimental_inline}}</dt>
 <dd>一个包含下列属性的对象:</dd>
 <dd>
 <dl>
  <dt><code>behavior</code> {{optional_inline}}</dt>
  <dd>定义动画过渡效果, <code>"auto"</code>或 <code>"smooth"</code> 之一。默认为 <code>"auto"</code></dd>
  <dt><code>block</code> {{optional_inline}}</dt>
  <dd>定义垂直方向的对齐, <code>"start"</code><code>"center"</code><code>"end"</code>, 或 <code>"nearest"</code>之一。默认为 <code>"start"</code></dd>
  <dt><code>inline</code> {{optional_inline}}</dt>
  <dd>定义水平方向的对齐, <code>"start"</code><code>"center"</code><code>"end"</code>, 或 <code>"nearest"</code>之一。默认为 <code>"nearest"</code></dd>
 </dl>
 </dd>
</dl>

<h2 id="示例">示例</h2>

<pre class="notranslate"><code>var element = document.getElementById("box");

element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({block: "end"});
element.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});</code></pre>

<h2 id="注意">注意</h2>

<p>取决于其它元素的布局情况,此元素可能不会完全滚动到顶端或底端。</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th>Specification</th>
   <th>Status</th>
   <th>Comment</th>
  </tr>
  <tr>
   <td>{{SpecName("CSSOM View", "#dom-element-scrollintoview", "Element.scrollIntoView()")}}</td>
   <td>{{Spec2("CSSOM View")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("api.Element.scrollIntoView")}}</p>

<h2 id="相关内容">相关内容</h2>

<ul>
 <li>{{domxref("Element.scrollIntoViewIfNeeded()")}}<a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded"> </a>{{non-standard_inline}}</li>
</ul>