iframe在chrome/safari中使用scrollheight调整大小

MB.*_*MB. 22 html javascript iframe resize

我正在尝试根据它的内容调整大小(变大或变小)iframe.单击每个页面后,将调用一个调整大小的方法.

在Chrome中,我可以使iframe更大,但不能更小.document.body.scrollHeight始终是最大的价值.

因此,如果一个大页面设置了#iframe.height ='620px',并且有人点击链接到页面的内容较少,则scrollHeight将保持在620px而不是减少.

在Chrome/Safari中处理此问题的正确方法是什么?

xpe*_*eta 24

在要求iframe内文档的高度之前,您应该将iframe对象的高度设置为"auto".像这样的东西:

objIframe = document.getElementById('theIframeId');    
objIframe.style.height = 'auto';
Run Code Online (Sandbox Code Playgroud)

现在:

document.body.scrollHeight
Run Code Online (Sandbox Code Playgroud)

返回文档的实际高度.

  • 如果有其他人遇到这个,为我修复的是为Firefox设置'document.body.scrollHeight',为Chrome设置'document.documentElement.scrollHeight'以及其他任何内容......我的示例使用postMessage进行跨域`if( isFirefox){e.source.postMessage('height:'+ document.body.scrollHeight,e.origin); } else {e.source.postMessage('height:'+ document.documentElement.scrollHeight,e.origin); }` (2认同)

Knu*_*Knu 11

你尝试使用了document.documentElement.scrollHeight吗?