iframe根据内容自适应高度,解决高度自适应和跳转页面高度被影响问题
相关代码
<body>
<iframe src="" id="iframeWorkPanel" name="iframeWorkPanel" width="100%" scrolling="no" frameborder="0" wmode="window" onload="reinitIframe(0)"></iframe>
</body>
<script>
window.onresize = function () {
reinitIframe(0); // 页面改变时调用
}
// iframe页面自适应
function reinitIframe(val) {
var iframe = document.getElementById("iframeWorkPanel");
if (val == 0) { // 当页面跳转时使iframe高度变0,去除之前高度对跳转页面的影响
iframe.height = 0;
}
try {
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
} catch (ex) {
}
}
window.setInterval("reinitIframe()", 350); // 定时触发,监控页面高度的改变
</script>