【问题标题】:Resize iframe height according to content height [duplicate]根据内容高度调整iframe高度[重复]
【发布时间】:2015-12-18 00:10:20
【问题描述】:

我正在尝试根据内容(网页)的高度和宽度调整 iframe 的大小。我使用了在堆栈的其他答案中找到的代码。对于新宽度的设置,似乎可行,但我无法使该高度起作用,我不知道为什么。

您可以在此处查看和编辑我的示例:http://jsfiddle.net/dzorz/pvtr3/

这是我的 HTML:

<iframe id="finance-iframe" class="finance-iframe" src="http://wordpress.org" width="100%" height="300px" marginheight="0" frameborder="0" onLoad="autoResize('finance-iframe');"></iframe>

和javascript:

function autoResize("finance-iframe"){
  var newheight;
  var newwidth;

  if(document.getElementById){
    newheight=document.getElementById("finance-iframe").contentWindow.document.body.scrollHeight;
    newwidth=document.getElementById("finance-iframe").contentWindow.document.body.scrollWidth;
  }

  document.getElementById("finance-iframe").height= (newheight) + "px";
  document.getElementById("finance-iframe").width= (newwidth) + "px";
}

我做错了什么?

【问题讨论】:

标签: javascript html css iframe


【解决方案1】:

要查看 iFrame 中内容的高度,需要查看 4 个不同的属性。

document.documentElement.scrollHeight
document.documentElement.offsetHeight
document.body.scrollHeight
document.body.offsetHeight

可悲的是,他们都可以给出不同的答案,而且这些在浏览器之间是不一致的。如果将正文边距设置为 0,则 document.body.offsetHeight 给出最佳答案。要获得正确的值,请尝试此功能;它取自 iframe-resizer 库,该库还负责在内容更改或调整浏览器大小时保持 iFrame 的正确大小。

function getIFrameHeight(){
    function getComputedBodyStyle(prop) {
        function getPixelValue(value) {
            var PIXEL = /^\d+(px)?$/i;

            if (PIXEL.test(value)) {
                return parseInt(value,base);
            }

            var 
                style = el.style.left,
                runtimeStyle = el.runtimeStyle.left;

            el.runtimeStyle.left = el.currentStyle.left;
            el.style.left = value || 0;
            value = el.style.pixelLeft;
            el.style.left = style;
            el.runtimeStyle.left = runtimeStyle;

            return value;
        }

        var 
            el = document.body,
            retVal = 0;

        if (document.defaultView && document.defaultView.getComputedStyle) {
            retVal =  document.defaultView.getComputedStyle(el, null)[prop];
        } else {//IE8 & below
            retVal =  getPixelValue(el.currentStyle[prop]);
        } 

        return parseInt(retVal,10);
    }

    return document.body.offsetHeight +
        getComputedBodyStyle('marginTop') +
        getComputedBodyStyle('marginBottom');
}

【讨论】:

    【解决方案2】:
    <section id="about" data-type="background" data-speed="10" class="pages">
        <iframe src="index.html" id="demo_frame" align="center" scrolling="no"  frameborder="0" marginheight="0" marginwidth="0"></iframe>
    </section>
    
    <script>
            $('iframe').load(function() {
                this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
            });
    </script>
    

    【讨论】:

    • 这应该是正确的答案。这是最优雅的。
    • 在我的情况下是行不通的
    猜你喜欢
    • 2012-08-03
    • 2010-12-18
    • 1970-01-01
    • 2012-05-21
    • 2014-11-06
    • 2012-07-29
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多