observe() 方法在 ResizeObserver interface starts observing the specified 元素 or SVGElement .

句法

resizeObserver.observe(target, options);
					

参数

target
A reference to an 元素 or SVGElement to be observed.
选项 可选
An options object allowing you to set options for the observation. Currently this only has one possible option that can be set:
box
Sets which box model the observer will observe changes to. Possible values are content-box (the default), and border-box .

返回值

Void.

异常

None.

范例

The following snippet is taken from the resize-observer-text.html ( see source ) example:

const resizeObserver = new ResizeObserver(entries => {
  for (let entry of entries) {
    if(entry.contentBoxSize) {
      h1Elem.style.fontSize = Math.max(1.5, entry.contentBoxSize.inlineSize/200) + 'rem';
      pElem.style.fontSize = Math.max(1, entry.contentBoxSize.inlineSize/600) + 'rem';
    } else {
      h1Elem.style.fontSize = Math.max(1.5, entry.contentRect.width/200) + 'rem';
      pElem.style.fontSize = Math.max(1, entry.contentRect.width/600) + 'rem';
    }
  }
});
resizeObserver.observe(divElem);
								

An observe() call with an options object would look like so:

resizeObserver.observe(divElem, { box : 'border-box' });
								

规范

规范 状态 注释
重置大小观测器
The definition of 'observe()' in that specification.
编者草案 初始定义。

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
observe Chrome 64 Edge 79 Firefox 69 IE No Opera 51 Safari 13.1 WebView Android 64 Chrome Android 64 Firefox Android No Opera Android 47 Safari iOS 13.4 Samsung Internet Android 9.0

图例

完整支持

完整支持

不支持

不支持

元数据

  • 最后修改:
  1. 重置尺寸观测器 API
  2. ResizeObserver
  3. 构造函数
    1. ResizeObserver()
  4. 方法
    1. disconnect()
    2. observe()
    3. unobserve()
  5. Related pages for Resize Observer API
    1. ResizeObserverEntry

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1