简体   繁体   English

iframe 主体上的 ResizeObserver 在 Chrome 和 Firefox 上的行为不同

[英]ResizeObserver on iframe body behaves differently on Chrome and Firefox

I'm trying to use ResizeObserver in order to listen for changes on the height of a document body and consequently adapt the size of the iframe which contains it.我正在尝试使用 ResizeObserver 来监听文档主体高度的变化,从而调整包含它的 iframe 的大小。

Everything seems to work fine on Chrome, but not on Firefox, where resize events do not seem to trigger the observer callback in the right way. Chrome 上似乎一切正常,但在 Firefox 上却不行,调整大小事件似乎不会以正确的方式触发观察者回调。

This is the observer code, which updates the container height on every content size change:这是观察者代码,它会在每次内容大小更改时更新容器高度:

const iframeElement = document.getElementById("container");
const content = iframeElement.contentWindow.document.body;

const resizeObserver = new ResizeObserver(entries => {
    entries.forEach(entry => {
        iframeElement.style.height = `${entry.target.scrollHeight}px`;
    });
});
resizeObserver.observe(iframeContent);

Here is a simple JSFiddle that clearly shows what I'm talking about: https://jsfiddle.net/ujxkre85/ .这是一个简单的 JSFiddle,它清楚地显示了我在说什么: https://jsfiddle.net/ujxkre85/

The containing iframe should adapt to the height of its content while this gets incremented.包含的 iframe 应该适应其内容的高度,而这会增加。 As you can see, it works fine on Chrome, but almost nothing happens running it on Firefox.如您所见,它在 Chrome 上运行良好,但在 Firefox 上运行几乎没有任何反应。

Is there something I'm not getting about the API or is Firefox missing something here?关于 API 或者 Firefox 在这里缺少什么,我是否有什么不明白的地方?

Thank you.谢谢你。

Firefox has a bug that is described here https://bugzilla.mozilla.org/show_bug.cgi?id=1689099 Firefox 有一个在此处描述的错误https://bugzilla.mozilla.org/show_bug.cgi?id=1689099

ResizeObserver callback doesn't reliably fire, when observing an element in an iframe subdocument (with the observer registered in outer document)观察 iframe 子文档中的元素时,ResizeObserver 回调不会可靠地触发(观察者在外部文档中注册)

So probably using MutationObserver is a best option.所以可能使用 MutationObserver 是最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM