iframe 自適應高度

<iframe id="iframe" src="xxxx.html" scrolling="no" width="100%"></iframe>

function reinitIframe(iframId) {
            var iframe = document.getElementById(iframId);
            console.log(iframe)
            try {
                var bHeight = iframe.contentWindow.document.body.scrollHeight;
                var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
                var height = Math.max(bHeight, dHeight);
                iframe.height = height;
                // console.log(iframe.contentWindow);
                // console.log(bHeight, dHeight);
            } catch (ex) {}
        };

 

setTimeout(function() {
     reinitIframe(iframeId)

}, 500);

 

如果沒效果,可適當加大 timeout 的時間

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