aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/resizeobserver/observe/index.html
blob: 310f527176ddc8afa41b53214617b3c52ddf5257 (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
---
title: ResizeObserver.observe()
slug: Web/API/ResizeObserver/observe
translation_of: Web/API/ResizeObserver/observe
---
<div>{{APIRef("Resize Observer API")}}{{SeeCompatTable}}</div>

<p class="summary">The <strong><code>observe()</code></strong> method of the {{domxref("ResizeObserver")}} interface initiates observing of a specified {{domxref('Element')}} or {{domxref('SVGElement')}}.<br>
 {{domxref("ResizeObserver")}} 接口的 <strong><code>observe()</code> </strong>方法用于观察一个指定{{domxref('Element')}} 或 {{domxref('SVGElement')}}</p>

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

<pre class="syntaxbox"><em>resizeObserver</em>.observe(<em>target</em>);</pre>

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

<dl>
 <dt>target</dt>
 <dd>被观察的 {{domxref('Element')}}{{domxref('SVGElement')}} 引用。</dd>
 <dt>options {{optional_inline}}</dt>
 <dd>一个指定观察设置的可选参数对象。目前只有一个可设置的选项:
    <dl>
      <dt><code>box</code></dt>
      <dd>设置观察者将以哪种盒子模型来观察变动。可以设置的值为
        <code>content-box</code> (默认值),<code>border-box</code>,和
        <code>device-pixel-content-box</code></dd>
    </dl>
  </dd>
</dl>

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

<p>{{jsxref('undefined')}}</p>

<h3 id="异常">异常</h3>

<p></p>

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

<p>以下示例通过观察box的尺寸变化从而改变其边框圆角半径。</p>

<pre class="brush: js">const resizeObserver = new ResizeObserver(entries =&gt; {
  for (let entry of entries) {
    entry.target.style.borderRadius = Math.max(0, 250 - entry.contentRect.width) + 'px';
  }
});
resizeObserver.observe(document.querySelector('.box:nth-child(2)'));</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Resize Observer','#dom-resizeobserver-observe','observe()')}}</td>
   <td>{{Spec2('Resize Observer')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

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



<p>{{Compat("api.ResizeObserver.observe")}}</p>