iframe自適應高度

        // 計算頁面的實際高度,iframe自適應會用到
        function calcPageHeight(doc) {
            var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
            var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight)
            var height = Math.max(cHeight, sHeight)
            console.log(height);
            return height
        }
        //根據ID獲取iframe對象

        window.onload = function () {
            onloadHeight();
        }

        function onloadHeight() {
            var ifr = document.getElementById("iframepage");
            //解決打開高度過高的頁面後再打開高度較小頁面滾動條不收縮
            ifr.height = 0;
            var iDoc = ifr.contentDocument || ifr.document
            var height = calcPageHeight(iDoc)
            if (height < 850) {
                height = 850;
            }
            ifr.height = height;
        }
相關文章
相關標籤/搜索