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

时间:2022-02-20 20:27:49

I'm trying to resize (make bigger or smaller) an iframe based on it's contents. After a click on each page a method is called which does the resizing.

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

In Chrome I can make the iframe bigger, but not smaller. document.body.scrollHeight is always the biggest value.

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

So if one big page sets #iframe.height = '620px', and someone clicks on a link to page with less content scrollHeight will remain at 620px instead of decreasing.

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

What's the proper way of handling this in Chrome/Safari?

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

2 个解决方案

#1


22  

Before you ask for the height of the document inside the iframe you should set the height of the iframe object to "auto". Something like this:

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

objIframe = document.getElementById('theIframeId');    
objIframe.style.height = 'auto';

And now:

现在:

document.body.scrollHeight

returns the actual height of the document.

返回文档的实际高度。

#2


10  

Did you try using document.documentElement.scrollHeight instead?

您是否尝试使用document.documentElement.scrollHeight?

#1


22  

Before you ask for the height of the document inside the iframe you should set the height of the iframe object to "auto". Something like this:

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

objIframe = document.getElementById('theIframeId');    
objIframe.style.height = 'auto';

And now:

现在:

document.body.scrollHeight

returns the actual height of the document.

返回文档的实际高度。

#2


10  

Did you try using document.documentElement.scrollHeight instead?

您是否尝试使用document.documentElement.scrollHeight?