根據iframe裏面的內容高度動態設置iframe高度

<iframe  id="stageFrame" onload="iframeLoad()" src="/home/index" style="width:100%;height:auto;overflow-x:hidden;overflow-y:hidden;"></iframe>

//根據iframe裏面的內容高度動態設置iframe高度
  function iframeLoad() {
        var ifr = document.getElementById('stageFrame');
        ifr.style.height =  '700px';
        var iDoc = ifr.contentDocument || ifr.contentWindow.document;
        //document.body.scrollHeight
        //var height = iDoc.documentElement.clientHeight || iDoc.body.clientHeight;
        var height = iDoc.body.scrollHeight;//爲防止iframe裏面出現滾動條,可以考慮在此基礎上加個100px,自己酌情決定
        //console.log(iDoc.documentElement.clientHeight, iDoc.body.clientHeight);
        ifr.style.height = height + 'px';
        console.log(height);
    }


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