aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/htmlelement/offsettop/index.html
blob: 449bfa2f686de83f01995e127ba21e9a0e8012f6 (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
---
title: HTMLElement.offsetTop
slug: Web/API/HTMLElement/offsetTop
translation_of: Web/API/HTMLElement/offsetTop
---
<div>{{ APIRef("HTML DOM") }}</div>

<p><strong><code>HTMLElement.offsetTop</code></strong> 为只读属性,它返回当前元素相对于其 {{domxref("HTMLElement.offsetParent","offsetParent")}} 元素的顶部内边距的距离。</p>

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

<pre class="syntaxbox"><var>topPos</var> = element.offsetTop;
</pre>

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

<ul>
 <li><code>topPos</code> 为返回的像素数。</li>
</ul>

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

<pre class="brush:js">var d = document.getElementById("div1");
var topPos = d.offsetTop;

if (topPos &gt; 10) {
  // div1 距离它的 offsetParent 元素的顶部的距离大于 10 px
}</pre>

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范名称</th>
   <th scope="col">规范状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSSOM View', '#dom-htmlelement-offsettop', 'offsetTop')}}</td>
   <td>{{Spec2('CSSOM View')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibility" name="Compatibility">Browser compatibility</h2>



<p>{{Compat("api.HTMLElement.offsetTop")}}</p>

<p>In compliance with the specification, this property will return <code>null</code> on Webkit if the element is hidden (the <code>style.display</code> of this element or any ancestor is <code>"none"</code>) or if the <code>style.position</code> of the element itself is set to <code>"fixed"</code>.</p>

<p>This property will return <code>null</code> on Internet Explorer (9) if the <code>style.position</code> of the element itself is set to <code>"fixed"</code>. (Having <code>display:none</code> does not affect this browser.)</p>