iframe包含在隐藏元素中的scrollHeight不正确

时间:2014-09-05 23:39:47

标签: javascript html iframe

我很抱歉我无法发布实际代码或实际工作示例。这个问题需要一个iframe所以我不能把小提琴放在一起,而且部署的服务器无法通过网络访问。希望我发布的这些片段足以复制。

我有一个页面需要在iframe中显示另一个页面(我无法控制),iframe的大小应根据其内容页面的高度而变化。用户以各种方式与之交互;调整大小总是由于隐藏/显示元素,但是,如果这有任何区别。

此调整大小行为正常(在所有浏览器中),但最初隐藏iframe包装器时除外。在这种情况下,如果iframe页面元素在Chrome中都可见,那么iframe的大小将达到目标网页的大小<(如此巨大...) ,或在FF中不可见(0高度)。

控制台中没有JS错误,并且两个页面都以其他方式相互访问(document.domain已设置)。该页面为XHTML Transitional,但如果它有所作为,我应该可以更改它。

iframe HTML:

<div id="frame-wrapper" style="display: none">
    <iframe src="some_page_that_shrinks_after_dom_load.html" frameborder="0" scrolling="no" onload="javascript:frameLoaded(this);"></iframe>
</div>

和JS:

function frameLoaded(iframe) {
    function resizeIframe() {
        iframe.style.height = iframe.contentWindow.document.documentElement.scrollHeight + 'px';
        alert(iframe.style.height);
    }

    // ...
    // set up some callbacks in the iframe to resizeIframe() which do work and give correct size, probably because the wrapper is unhidden
    // ...

    // this call for the size at the time of load is what is broken iff "frame-wrapper" is display: none
    resizeIframe();
    // the hack im using to avoid this:
    $('#frame-wrapper').show();
    resizeIframe();
    $('#frame-wrapper').hide();
}

如你所见,我已经知道如何解决这个问题。我真正想要的是解释这里发生了什么。

0 个答案:

没有答案