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;
        }
//子页修改父页高度 得到自适应
      $(window.parent.document).find("#iframepage")[0].style.height = height+"px";

  

原文地址:https://www.cnblogs.com/Harvard-L/p/9485535.html