iframe高度處理

weixin_34104341發表於2020-04-07

1.內容寬度變化的iframe高度自適應

例子如下:

<iframe src="a.html" frameborder="0" scrolling="no" id="test" onload="this.height=500"></iframe>
<script type="text/javascript">
function reinitIframe(){
var iframe = document.getElementById("test");
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(height);
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);
</script>

2.針對知道的iframe的ID呼叫

例子如下:

function iframeAutoFit(iframeObj){
setTimeout(function(){if(!iframeObj) return;iframeObj.height=(iframeObj.Document?iframeObj.Document.body.scrollHeight:iframeObj.contentDocument.body.offsetHeight);},200)
}

轉載於:https://www.cnblogs.com/zhaoyiguang/p/JavaScript.html

相關文章