--- title: ResizeObserver.observe() slug: Web/API/ResizeObserver/observe translation_of: Web/API/ResizeObserver/observe ---
The observe()
method of the {{domxref("ResizeObserver")}} interface initiates observing of a specified {{domxref('Element')}} or {{domxref('SVGElement')}}.
{{domxref("ResizeObserver")}} 接口的 observe()
方法用于观察一个指定{{domxref('Element')}} 或 {{domxref('SVGElement')}}。
resizeObserver.observe(target);
box
content-box
(默认值),border-box
,和
device-pixel-content-box
。{{jsxref('undefined')}}
无
以下示例通过观察box的尺寸变化从而改变其边框圆角半径。
const resizeObserver = new ResizeObserver(entries => { 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)'));
Specification | Status | Comment |
---|---|---|
{{SpecName('Resize Observer','#dom-resizeobserver-observe','observe()')}} | {{Spec2('Resize Observer')}} | Initial definition. |
{{Compat("api.ResizeObserver.observe")}}