aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/range/getboundingclientrect
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/range/getboundingclientrect
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/range/getboundingclientrect')
-rw-r--r--files/zh-cn/web/api/range/getboundingclientrect/index.html90
1 files changed, 90 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/range/getboundingclientrect/index.html b/files/zh-cn/web/api/range/getboundingclientrect/index.html
new file mode 100644
index 0000000000..4c10f536b0
--- /dev/null
+++ b/files/zh-cn/web/api/range/getboundingclientrect/index.html
@@ -0,0 +1,90 @@
+---
+title: Range.getBoundingClientRect()
+slug: Web/API/Range/getBoundingClientRect
+tags:
+ - API
+ - CSSOM View
+ - Method
+ - Range
+ - 域
+ - 方法
+ - 范围
+translation_of: Web/API/Range/getBoundingClientRect
+---
+<div>{{ApiRef("DOM")}}{{SeeCompatTable}}</div>
+
+<p><strong><code>Range.getBoundingClientRect()</code></strong> 返回一个 {{ domxref("DOMRect") }} 对象,该对象将范围中的内容包围起来;即该对象是一个将范围内所有元素的边界矩形包围起来的矩形(译者注:关于边界矩形,可以参考 <a href="https://en.wikipedia.org/wiki/Minimum_bounding_rectangle">Minimum Bouding Rectangles</a>)。</p>
+
+<p>此方法可用于确定文本区域中选中的部分或光标的视窗坐标。关于返回值的详细信息,参见 {{domxref("Element.getBoundingClientRect()")}}。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox notranslate"><var>boundingRect</var> = <var>range</var>.getBoundingClientRect()
+</pre>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="HTML">HTML</h3>
+
+<pre class="brush: html notranslate">&lt;div id="highlight"&gt;&lt;/div&gt;
+&lt;p&gt;This example positions a "highlight" rectangle behind the contents of a range. The range's content &lt;b&gt;starts here&lt;/b&gt; and continues on until it &lt;b&gt;ends here&lt;/b&gt;. The bounding client rectangle contains everything selected in the range.&lt;/p&gt;</pre>
+
+<h3 id="CSS">CSS</h3>
+
+<pre class="brush: css notranslate">#highlight {
+ background: yellow;
+ position: absolute;
+ z-index: -1;
+}
+
+p {
+ width: 200px;
+}</pre>
+
+<h3 id="JavaScript">JavaScript</h3>
+
+<pre class="brush: js notranslate">const range = document.createRange();
+range.setStartBefore(document.getElementsByTagName('b').item(0), 0);
+range.setEndAfter(document.getElementsByTagName('b').item(1), 0);
+
+const clientRect = range.getBoundingClientRect();
+const highlight = document.getElementById('highlight');
+highlight.style.left = `${clientRect.x}px`;
+highlight.style.top = `${clientRect.y}px`;
+highlight.style.width = `${clientRect.width}px`;
+highlight.style.height = `${clientRect.height}px`;</pre>
+
+<h3 id="结果">结果</h3>
+
+<p><iframe class="live-sample-frame sample-code-frame" frameborder="0" id="frame_Example" src="https://mdn.mozillademos.org/en-US/docs/Web/API/Range/getBoundingClientRect$samples/Example?revision=1549349"></iframe></p>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('CSSOM View', '#dom-range-getboundingclientrect', 'Range.getBoundingClientRect()')}}</td>
+ <td>{{Spec2('CSSOM View')}}</td>
+ <td>Initial specification.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.Range.getBoundingClientRect")}}</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>{{domxref("Range.getClientRects()")}} - 对于非矩形的范围(如选择部分换行了)会返回更细致的结果。</li>
+ <li>{{domxref("Element.getBoundingClientRect()")}}</li>
+ <li>{{domxref("Document.caretPositionFromPoint()")}} - 使用视窗坐标获取节点范围。</li>
+</ul>