解决Iframe 高度自适应 , iframe内的内容可动态修改

在不用框架时,头, 尾一般以公共资源引入 ,footer 的内容时异步刷新的所以高度是不定的 ,所以iframe 的高度也需要自适应
html
<iframe id="frameFooter" class="homeIframefooter" src="../public/footer.html" frameborder="0" scrolling="no"></iframe>
js
<script type="text/javascript"> function reinitIframe(){ var iframe = document.getElementById("frameFooter"); try{ var bHeight = iframe.contentWindow.document.body.scrollHeight; // console.log("bHeight",bHeight) var dHeight = iframe.contentWindow.document.documentElement.scrollHeight; // console.log("dHeight",dHeight) if(dHeight>bHeight){ iframe.height = bHeight; }else{ iframe.height = height; } }catch (ex){} } window.setInterval("reinitIframe()", 200); </script>

posted @ 2021-02-25 10:02  写bug的靓仔  阅读(257)  评论(0)    收藏  举报