iframe高度自適應子頁面

      最新做的項目採用的iframe,但是在使用bootstrapTable這類高度動態變化時,iframe的高度要麼太高要麼太低導致頁面被遮擋,而且頁面也不美觀。網上也找了很多方案但是結果都是不太理想,結合其他人的經驗總結下自己的這個方法。

        //動態判定iframe頁面高度
        function setIframeHeight() {
            var iframe = document.getElementById('mainFrame');// mainFrame爲當前的iframe的id
            if (iframe) {
                var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
                if (iframeWin.document.body) {
                    iframe.height = iframeWin.document.body.scrollHeight || iframeWin.document.documentElement.scrollHeight;
                }
            }
        };
        window.onload = function () {
            setIframeHeight(); 
        };
        window.setInterval("setIframeHeight()", 200);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章