返回顶部

iframe高度处理

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)
}
posted @   gz_zyg  阅读(166)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示