iframe 根據內容自適應高度-終極解決方案

第一中方法:javascript

在子頁面加載完畢的時候執行java

parent.document.getElementById("iframe").height=0;spa

parent.document.getElementById("iframe").height=document.body.scrollHeight;code


第二中方法:blog

在主頁面 iframe onLoad 時間裏面寫ip

function iframeLoad()
{
    document.getElementById("iframe").height=0;
        
   document.getElementById("iframe").height=document.getElementById("iframe").contentWindow.document.body.scrollHeight;
}    

 

第三種方法:get

使用js在頁面加載完成後設置寬高度iframe

iframeit

<iframe name="menuFrame" id="menuFrame" onload="reinitIframe()" style="overflow:visible;"
       scrolling="no" height="100%" width="100%">
</iframe>

javascriptio

        window.onresize = function () {
            reinitIframe();
        }
        function reinitIframe(){
            var iframe = document.getElementById("menuFrame");
            try{
                var bHeight = iframe.contentWindow.document.body.scrollHeight;
                var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
                var height = Math.min(bHeight, dHeight);
                iframe.height = height+50;
                // console.log(iframe.height);
            }catch (ex){}
        }
        // 定時觸發
        window.setInterval("reinitIframe()", 200);
相關文章
相關標籤/搜索