【问题标题】:How to auto-size an iFrame? [duplicate]如何自动调整 iFrame 的大小? [复制]
【发布时间】:2010-09-19 19:32:41
【问题描述】:

可能重复:
Resizing an iframe based on content

我正在加载 iFrame,并希望父级根据 iFrame 内容的高度自动更改高度。

简单来说,所有页面都属于同一个域,所以我不应该遇到跨站点脚本问题。

【问题讨论】:

标签: javascript iframe autosize


【解决方案1】:

在任何其他元素上,我会使用 DOM 对象的 scrollHeight 并相应地设置高度。我不知道这是否适用于 iframe(因为它们对所有内容都有些古怪),但它当然值得一试。

编辑:环顾四周,普遍的共识是使用 offsetHeight 设置 iframe 内的高度:

function setHeight() {
    parent.document.getElementById('the-iframe-id').style.height = document['body'].offsetHeight + 'px';
}

并附加它以与 iframe-body 的 onLoad 事件一起运行。

【讨论】:

  • 这对我不起作用。为了清楚起见,是否应该在 IFRAME HTML 中定义 setHeight()?但是-iframe-id是主页面html中定义的iframe的id?
  • 是的,在 IE 中也无法正常工作……它只是根据浏览器的高度调整大小。
  • 添加 +- 60 像素的偏移量以确保滚动条不可见(不同的浏览器)并不是一个坏主意。
  • 上述代码有错误。 parent.document.getElementById('the-iframe-id').height 应改为 parent.document.getElementById('the-iframe-id').style.height。请注意,heightstyle 属性的属性。
  • 在 Firefox、Chrome、Opera 上测试它的工作! +1
【解决方案2】:

【讨论】:

  • 谁能告诉我如何去除这个插件可怕的双刷新效果?例如:sample
  • 这个插件现在似乎运行良好
  • 太棒了,谢谢!!!
  • 我希望当 iframe.height = iframe.scrollHeight 不起作用时,会有一个复杂的插件来代替
  • 更多也适用于跨域的更新解决方案是:github.com/davidjbradshaw/iframe-resizer
【解决方案3】:

我刚好遇到你的问题,我有一个解决方案。但它在jquery中。太简单了。

$('iframe').contents().find('body').css({"min-height": "100", "overflow" : "hidden"});
setInterval( "$('iframe').height($('iframe').contents().find('body').height() + 20)", 1 );

你去!

干杯! :)

编辑:如果您有一个基于 iframe 方法而不是 div 方法的富文本编辑器,并且希望它扩展每个新行,那么这段代码就可以满足需要。

【讨论】:

  • 很好,但如果有多个 iframe 则不起作用。查看我的解决方案 ;-)
  • 我使用了这段代码并加载它继续扩展 iframe...
  • 你真的不应该使用 eval'd 代码作为 setInterval 函数的第一个参数。而是使用回调。
  • 有谁知道为什么 iframe 在某些页面中不断扩展框架,但不是全部?如何预防?
【解决方案4】:

这是一个非常简单的解决方案,适用于所有浏览器和跨域:

首先,这适用于以下概念:如果包含 iframe 的 html 页面设置为 100% 的高度,并且使用 css 设置 iframe 的样式以具有 100% 的高度,那么 css 将自动调整所有内容的大小以适应。

代码如下:

<head>
<style type="text/css"> 
html {height:100%}
body {
margin:0;
height:100%;
overflow:hidden
}
</style>
</head>

<body>
<iframe allowtransparency=true frameborder=0 id=rf sandbox="allow-same-origin allow-forms allow-scripts" scrolling=auto src="http://www.externaldomain.com/" style="width:100%;height:100%"></iframe>
</body>

【讨论】:

  • 当 iframe 不是 body 的直接子代而是 div(即在所有浏览器上)时,这是否也有效?
  • 是的 - 只需使用 css 设置 div 的高度和宽度,然后将 iframe 放入您的 div 中,瞧,应该只有一组滚动条(除非您将 div 设置为 100%页面的高度)。
  • 不,没有。 FF 3.6 赢 xp
  • 不,也不能在 chrome 中工作。
  • 看起来这个 2010 年的答案(!)不再正确。
【解决方案5】:

这个solution 最适合我。它使用 jQuery 和 iframe 的“.load”事件。

【讨论】:

    【解决方案6】:

    在 IE 5.5+ 中,您可以使用 contentWindow 属性:

    iframe.height = iframe.contentWindow.document.scrollHeight;
    

    在 Netscape 6(假设也是 firefox)中,contentDocument 属性:

    iframe.height = iframe.contentDocument.scrollHeight
    

    【讨论】:

      【解决方案7】:

      我发现@ShripadK 的解决方案最有帮助,但它没有 工作,如果有多个 iframe。我的解决方法是:

      function autoResizeIFrame() {
        $('iframe').height(
          function() {
            return $(this).contents().find('body').height() + 20;
          }
        )
      }
      
      $('iframe').contents().find('body').css(
        {"min-height": "100", "overflow" : "hidden"});
      
      setTimeout(autoResizeIFrame, 2000);
      setTimeout(autoResizeIFrame, 10000);
      
      • $('iframe').height($('iframe').contents().find('body').height() + 20) 将设置 将每一帧的高度设为相同的值,即第一帧内容的高度。 所以我使用 jquery 的 height() 和一个函数而不是一个值。这样个人 计算高度
      • + 20 是一种解决 iframe 滚动条问题的技巧。该数字必须大于滚动条的大小。黑客可能会 应避免但禁用 iframe 的滚动条。
      • 我使用 setTimeout 而不是 setInterval(..., 1) 来减少我的 CPU 负载

      【讨论】:

        【解决方案8】:

        我的解决方案,(使用 jquery):

        <iframe id="Iframe1" class="tabFrame" width="100%" height="100%" scrolling="no" src="http://samedomain" frameborder="0" >
        </iframe>  
        <script type="text/javascript">
            $(function () {
        
                $('.tabFrame').load(function () {
                    var iframeContentWindow = this.contentWindow;
                    var height = iframeContentWindow.$(document).height();
                    this.style.height = height + 'px';
                });
            });
        </script>
        

        【讨论】:

        • 在小修复对我有用之后: $('iframe').load(function() { var iframeContentWindow = this.contentWindow; var height = $(iframeContentWindow.document).height(); this .style.height = 高度 + 'px'; });
        • 还有一个缺失的 });最后还有;)
        【解决方案9】:

        Oli 有一个适合我的解决方案。作为记录,我的 iFrame 中的页面是由 javascript 呈现的,所以在报告 offsetHeight 之前我需要一个无限小的延迟。它看起来像这样:

        
            $(document).ready(function(){
                setTimeout(setHeight);
            });
        
            function setHeight() {
                alert(document['body'].offsetHeight);   
            }
        

        【讨论】:

          【解决方案10】:

          这是我使用原型发现的最简单的方法:

          Main.html:

          <html>
          <head>
          
          
          <script src="prototype.js"></script>
          
          <script>
              function init() {
                  var iframe = $(document.getElementById("iframe"));
                  var iframe_content = $(iframe.contentWindow.document.getElementById("iframe_content"));
                  var cy = iframe_content.getDimensions().height;
                  iframe.style.height = cy + "px";
              }
          </script>
          
          </head>
          
          <body onload="init()">
          
          
          <iframe src="./content.html" id="iframe" frameBorder="0" scroll="no"></iframe>
          
          <br>
          this is the next line
          
          </body>
          </html>
          

          content.html:

          <html>
          <head>
          <script src="prototype.js"></script>
          
          
          <style>
          body {
              margin: 0px;
              padding: 0px;
          }
          </style>
          
          
          </head>
          
          <body>
          
          
          <div id="iframe_content" style="max-height:200px;">
          Sub content<br>
          Sub content<br>
          ...
          ...
          ...
          </div>
          
          
          </body>
          </html>
          

          这似乎(到目前为止)在所有主要浏览器中都有效。

          【讨论】:

          • 误解了这个问题,很公平。上面的代码会将 iframe 调整为自己的内容大小(也可能对某人有用)
          【解决方案11】:

          我的解决方法是将 iframe 的高度/宽度设置为远远超过 CSS 中任何预期的源页面大小,并将 background 属性设置为 transparent

          在 iframe 中将allow-transparency 设置为truescrolling 设置为no

          唯一可见的是您使用的任何源文件。它适用于 IE8、Firefox 3 和 Safari。

          【讨论】:

            【解决方案12】:

            实际上——Patrick 的代码也对我有用。正确的做法是这样的:

            注意:前面有一点jquery:

            
            if ($.browser.msie == false) {
                var h = (document.getElementById("iframeID").contentDocument.body.offsetHeight);
            } else {
                var h = (document.getElementById("iframeID").Document.body.scrollHeight);
            }
            

            【讨论】:

            • 帕特里克在哪里回答?
            • 为什么不交换两者并摆脱== false?如果没有,您应该在开头添加一个! (if (!$.browser.msie) {)。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-27
            • 2011-04-26
            • 1970-01-01
            • 1970-01-01
            • 2021-06-30
            相关资源
            最近更新 更多