iframe高度自适应方法

    <iframe width="100%" id="tbbrecommend" name="tbbrecommend" src="test.php" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></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)
        return height
    }
    var ifr = document.getElementById('tbbrecommend')
    ifr.onload = function() {
        var iDoc = ifr.contentDocument || ifr.document
        var height = calcPageHeight(iDoc)
        ifr.style.height = height + 'px'
    }

以上方法在pc测试没有问题,但是在手机浏览器删查看则没有效果

原文地址:https://www.cnblogs.com/dongruiha/p/7550652.html