自动高度iframe到内容

时间:2014-08-06 15:45:00

标签: iframe height

只是无法弄清楚如何让下面的代码自动调整高度,取决于" src"中的路径。因为我不想要硬编码,src路径的高度可能会改变。我尝试了各种js方法,但似乎都没有。

<iframe src="C:\" height="200" width="100%"></iframe>

我已经完成了内联样式,但是将管理css文件中的所有样式。

帮助。

2 个答案:

答案 0 :(得分:1)

您需要使用JavaScript调整iFrame以匹配内容的大小。查看此库,该库将监视iFrame中的内容并保持大小最新。

https://github.com/davidjbradshaw/iframe-resizer

答案 1 :(得分:1)

<script language="javascript" type="text/javascript">
function resizeIframe(obj) {
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
</script>

<iframe  src="http://stackoverflow.com/" frameborder="0" scrolling="no" id="iframe" onload='javascript:resizeIframe(this);' />