javascript 装载iframe子页面,自适应高度

2019-12-25,,,,

假设主页面有一个div,里面放置一个iframe
复制代码 代码如下:
<div id="frameBox">
<iframe id="frameWin" src="1.html" name="opWin" style="width:100%; height:100% " frameborder="0" scrolling="no"></iframe>
</div>

3个菜单链接,分别在iframe加载 1.html、2.html、3.html 三个页面。

3个子页面分别在自己页面加载完window.onload执行
复制代码 代码如下:
function aa(){
var newHeight = document.body.scrollHeight + 20 + "px";
window.parent.document.getElementById("frameBox").style.height = newHeight;
//以上firefox通过,但是ie6必须加上下面这句,不然iframe高度是改了,但是可见区域没有改
window.parent.document.getElementById("frameWin").style.height = newHeight;
}

以下方法只需要把代码放在主页面:
页面代码:
复制代码 代码如下:
<div style="border:1px solid #7e99c6" id="frameBox">
<iframe id="frameWin" src="01.html" name="opWin" style="width:100%; height:100% " frameborder="0" scrolling="no" onload="test2()"></iframe>
</div>

js脚本(加在主页面):
复制代码 代码如下:
function test2(){
var frameWin = document.getElementById("frameWin");
var frameBox = document.getElementById("frameBox");
var newHeight;
if (frameWin.Document){
newHeight = frameWin.Document.body.scrollHeight + 20 + "px";
}else{
newHeight = frameWin.contentDocument.body.scrollHeight+ 20 + "px";
}
frameWin.style.height = newHeight;
frameBox.style.height = newHeight;
}

您可能感兴趣的文章:

  • js实现背景图自适应窗口大小
  • js实现文本框宽度自适应文本宽度的方法
  • JS制作手机端自适应缩放显示
  • js实现屏幕自适应局部代码分享
  • JS获取图片实际宽高及根据图片大小进行自适应
  • javascript自适应宽度的瀑布流实现思路
  • Iframe 自适应高度并实时监控高度变化的js代码
  • JS实现iframe自适应高度的方法示例
  • 通过JAVAScript实现页面自适应
  • 几行js代码实现自适应

《javascript 装载iframe子页面,自适应高度.doc》

下载本文的Word格式文档,以方便收藏与打印。