blob: a3bd445ea732aca02b6ae0a31c40252722017152 (
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
|
---
title: HTMLElement.offsetLeft
slug: Web/API/HTMLElement/offsetLeft
tags:
- API
- CSSOM View
- NeedsMarkupWork
- Property
- Read-only
- Reference
browser-compat: api.HTMLElement.offsetLeft
translation_of: Web/API/HTMLElement/offsetLeft
---
<div>{{ APIRef("HTML DOM") }}</div>
<p><strong><code>HTMLElement.offsetLeft</code></strong> プロパティは読み取り専用で、現在の要素の<em>左上隅</em>が {{domxref("HTMLElement.offsetParent")}} ノード内で左へオフセットされるピクセル数を返します。</p>
<p>ブロックレベル要素では、 <code>offsetTop</code>, <code>offsetLeft</code>, <code>offsetWidth</code>, <code>offsetHeight</code> が <code>offsetParent</code> からの相対的な要素の境界ボックスを記述します。</p>
<p>しかし、 (<strong>span</strong> などの) インラインレベル要素は行をまたいで折り返すことがあるので、 <code>offsetTop</code> および <code>offsetLeft</code> は<em>最初の</em>境界ボックス (その幅と高さを取得するには {{domxref("Element.getClientRects()")}} を使用) の位置を記述するのに対し、 <code>offsetWidth</code> および <code>offsetHeight</code> は<em>囲む</em>境界ボックス (位置を取得するには {{domxref("Element.getBoundingClientRect()")}} を使用) の寸法を記述します。したがって、 <code>offsetLeft</code>, <code>offsetTop</code>, <code>offsetWidth</code>, <code>offsetHeight</code> による左、上、幅、高さは折り返されたテキストの区間を囲むボックスにはなりません。</p>
<h2 id="Syntax">構文</h2>
<pre class="eval"><var>left</var> = <var>element</var>.offsetLeft;
</pre>
<p><code><var>left</var></code> は整数で、<em>最も近くの相対配置された</em>親要素からの左へのオフセットをピクセル数で表します。</p>
<h2 id="Example">例</h2>
<pre class="brush: js">var colorTable = document.getElementById("t1");
var tOLeft = colorTable.offsetLeft;
if (tOLeft > 5) {
// large left offset: do something here
}
</pre>
<p>この例は、 div 内で折り返す「長い」文を青い境界線で表示し、 span の境界を記述すると考えられるものを赤いボックスで表示します。</p>
<p><img src="offsetleft.jpg"></p>
<pre class="brush: html"><div style="width: 300px; border-color:blue; border-style:solid; border-width:1;">
<span>Short span. </span>
<span id="longspan">Long span that wraps within this div.</span>
</div>
<div id="box" style="position: absolute; border-color: red; border-width: 1; border-style: solid; z-index: 10">
</div>
<script type="text/javascript">
var box = document.getElementById("box");
var longspan = document.getElementById("longspan");
box.style.left = longspan.offsetLeft + document.body.scrollLeft + "px";
box.style.top = longspan.offsetTop + document.body.scrollTop + "px";
box.style.width = longspan.offsetWidth + "px";
box.style.height = longspan.offsetHeight + "px";
</script></pre>
<h2 id="Specification">仕様書</h2>
{{Specifications}}
<h2 id="Browser_compatibility">ブラウザーの互換性</h2>
<p>{{Compat}}</p>
<h2 id="See_also">関連情報</h2>
<ul>
<li>{{domxref("HTMLElement.offsetParent")}}, {{domxref("HTMLElement.offsetTop")}}, {{domxref("HTMLElement.offsetWidth")}}, {{domxref("HTMLElement.offsetHeight")}}</li>
</ul>
|