Help us test MDN's new front-end: https://discourse.mozilla.org/t/help-us-test-mdns-new-react-front-end-beta/42593

ResizeObserver.ResizeObserver()

这篇翻译不完整。请帮忙从英语翻译这篇文章

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

ResizeObserver 构造器创新一个新的  ResizeObserver 对象,用于接收 Element内容区域的改变 或 SVGElement 的边界框改变改变。

语法

var ResizeObserver = new ResizeObserver(callback)

参数

回调函数
The method called whenever a resize occurs. The method is called with an array of ResizeObserverEntry objects.
当尺寸发生变化时触发回调,使用ResizeObserverEntry对象数组调用该方法。

示例

The following example shows the dimensions of a box inside it, as text, upon resizing.
下面示例展示了box调整大小时,其内部文本显示为尺寸大小。

const resizeObserver = new ResizeObserver(entries => {
  for (let entry of entries) {
    const boxEl = entry.target;
    const dimensions = entry.contentRect;
    boxEl.textContent = `${dimensions.width} x ${dimensions.height}`;
  }
});
resizeObserver.observe(document.querySelector('.box:nth-child(2)'));

规范

Specification Status Comment
Resize Observer
ResizeObserver
Editor's Draft Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
ResizeObserver() constructor
Experimental
Chrome Full support 64Edge No support NoFirefox No support NoIE No support NoOpera Full support 51Safari No support NoWebView Android Full support 64Chrome Android Full support 64Edge Mobile No support NoFirefox Android No support NoOpera Android Full support 47Safari iOS No support NoSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

文档标签和贡献者

此页面的贡献者: vaynewang
最后编辑者: vaynewang,