zoukankan      html  css  js  c++  java
  • IFRAME高度自适应——框架高度等于其页面高度

    <iframe id="mainframe" name="mainframe" src="services/services.asp?owen1=服务理念" onload="this.style.height = parseInt(window.frames['mainframe'].document.body.scrollHeight);" frameborder="0" marginheight="0" marginwidth="0">
    </iframe>

    这段代码是页面加载时自适应,当页面内部发生变化时用以下代码自适应:
    $(window).resize(function(){
    parent.$("#mainframe").height($("body").height());
    });

    以下为非原始JS代码:
    方法一:
    function SetWinHeight(obj)
    {
        var win=obj;
        if (document.getElementById)
        {
            if (win && !window.opera)
            {
                if (win.contentDocument && win.contentDocument.body.offsetHeight) 
                    win.height = win.contentDocument.body.offsetHeight;
                else if(win.Document && win.Document.body.scrollHeight)
                    win.height = win.Document.body.scrollHeight;
            }
        }
    }

    <iframe width="778" align="center" height="200" id="win" name="win" onload="Javascript:SetWinHeight(this)" frameborder="0" scrolling="no"></iframe>

    方法二:
    <script type="text/javascript">
    //** iframe自动适应页面 **//

    //输入你希望根据页面高度自动调整高度的iframe的名称的列表
    //用逗号把每个iframe的ID分隔. 例如: ["myframe1", "myframe2"],可以只有一个窗体,则不用逗号。

    //定义iframe的ID
    var iframeids=["test"]

    //如果用户的浏览器不支持iframe是否将iframe隐藏 yes 表示隐藏,no表示不隐藏
    var iframehide="yes"

    function dyniframesize()
    {
    var dyniframe=new Array()
    for (i=0; i<iframeids.length; i++)
    {
    if (document.getElementById)
    {
    //自动调整iframe高度
    dyniframe[dyniframe.length] = document.getElementById(iframeids);
    if (dyniframe && !window.opera)
    {
    dyniframe.style.display="block"
    if (dyniframe.contentDocument && dyniframe.contentDocument.body.offsetHeight) //如果用户的浏览器是NetScape
    dyniframe.height = dyniframe.contentDocument.body.offsetHeight;
    else if (dyniframe.Document && dyniframe.Document.body.scrollHeight) //如果用户的浏览器是IE
    dyniframe.height = dyniframe.Document.body.scrollHeight;
    }
    }
    //根据设定的参数来处理不支持iframe的浏览器的显示问题
    if ((document.all || document.getElementById) && iframehide=="no")
    {
    var tempobj=document.all? Document.all[iframeids] : document.getElementById(iframeids)
    tempobj.style.display="block"
    }
    }
    }

    if (window.addEventListener)
    window.addEventListener("load", dyniframesize, false)
    else if (window.attachEvent)
    window.attachEvent("onload", dyniframesize)
    else
    window.onload=dyniframesize
    </script>

    方法三:
    function SetCwinHeight(iframeObj) {
                if (document.getElementById) {
                    if (iframeObj) {
                        if (iframeObj.contentDocument && iframeObj.contentDocument.body.offsetHeight) {
                            iframeObj.height = iframeObj.contentDocument.body.offsetHeight + 30;
                        } else if (document.frames[iframeObj.name].document && document.frames[iframeObj.name].document.body.scrollHeight) {
                            iframeObj.height = document.frames[iframeObj.name].document.body.scrollHeight + 30;
                        }
                    }
                }
            }
  • 相关阅读:
    esper(4-5)- Context 条件
    esper(4-4)-OverLapping Context
    esper(4-3)-Non-Overlapping Context
    esper(4-2)-Category Context
    esper(4-1)-简单context
    esper(3)-窗口&聚合分组
    esper(2)-事件类型
    java常用代码
    idea(3)-jetty配置
    BP(反向传播)算法原理及推导
  • 原文地址:https://www.cnblogs.com/cosiray/p/1551272.html
Copyright © 2011-2022 走看看