iframe的下拉菜单

来源:互联网 发布:linux can总线 编辑:程序博客网 时间:2024/03/29 13:24

Demo页面:主页面 iframe_a.html ,被包含页面 iframe_b.htm 和 iframe_c.html

下面开始讲:

 

通过Google搜索iframe 自适应高度,结果5W多条,搜索iframe 高度自适应,结果2W多条。
我翻了前面的几十条,刨去大量的转载,有那么三五篇是原创的。而这几篇原创里面,基本上只谈到如何自适应静的东西,就是没有考虑到JS操作DOM之后,如何做动态同步的问题。另外,在兼容性方面,也研究的不彻底。

这篇文章,希望在这两个方面再做一些深入。

 

可能有人还没接触到这个问题过,先说明一下,什么是自适应高度吧。所谓iframe自适应高度,就是,基于界面美观和交互的考虑,隐藏了 iframe的border和scrollbar,让人看不出它是个iframe。如果iframe始终调用同一个固定高度的页面,我们直接写死 iframe高度就可以了。而如果iframe要切换页面,或者被包含页面要做DOM动态操作,这时候,就需要程序去同步iframe高度和被包含页的实际高度了。

顺便说下,iframe在迫不得已的时候才去用,它会给前端开发带来太多的麻烦。

 

传统做法大致有两个:
方法一,在每个被包含页在本身内容加载完毕之后,执行JS取得本页面的高度,然后去同步父页面的iframe高度。

方法二,在主页面iframe的onload事件中执行JS,去取得被包含页的高度内容,然后去同步高度。
在代码维护角度考虑,方法二是优于方法一的,因为方法一,每个被包含页都要去引入一段相同的代码来做这个事情,创建了好多副本。

 

两个方法都只处理了静的东西,就是只在内容加载的时候执行,如果JS去操作DOM引起的高度变化,都不太方便。

如果在主窗口做一个Interval,不停的来获取被包含页的高度,然后做同步,是不是即方便,又解决了JS操作DOM的问题了呢?答案是肯定的。

 

Demo页面:主页面 iframe_a.html ,被包含页面 iframe_b.htm 和 iframe_c.html

主页面代码示例:

 


Js代码 复制代码
<iframe id="frame_content" src="iframe_b.html" scrolling="no" frameborder="0"></iframe><script type="text/javascript">   
  
function reinitIframe(){   
  
var iframe = document.getElementById("frame_content");   
  
try{   
  
iframe.height =  iframe.contentWindow.document.documentElement.scrollHeight;   
  
}catch (ex){}   
  
}   
  
window.setInterval("reinitIframe()", 200);   
  
</script>  
<iframe id="frame_content" src="iframe_b.html" scrolling="no" frameborder="0"></iframe><script type="text/javascript">    function reinitIframe(){    var iframe = document.getElementById("frame_content");    try{    iframe.height =  iframe.contentWindow.document.documentElement.scrollHeight;    }catch (ex){}    }    window.setInterval("reinitIframe()", 200);    </script>

 

一直执行,效率会不会有问题?
我做了测试,同时开5个窗口(IE6、IE7、FF、Opera、Safari)执行这个代码,不会对CPU有什么影响,甚至调整到2ms,也没影响(基本维持在0%占用率)。

 

下面谈谈各浏览器的兼容性问题,如何获取到正确的高度,主要是对body.scrollHeight和 documentElement.scrollHeight两个值得比较。注意本文用的是这个doctype,不同的doctype应该不会影响结果,但是假如你的页面没有申明doctype,那还是先去加一个吧。

 


Html代码 复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

 

在主页面追加以下测试代码,以输出这两个值,代码示例:

 


Js代码 复制代码
<div><button onclick="checkHeight()">Check Height</button></div><script type="text/javascript">   
  
function checkHeight() {   
  
var iframe = document.getElementById("frame_content");   
  
var bHeight = iframe.contentWindow.document.body.scrollHeight;   
  
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;   
  
alert("bHeight:" + bHeight + ", dHeight:" + dHeight);   
  
}   
  
</script>  
<div><button onclick="checkHeight()">Check Height</button></div><script type="text/javascript">    function checkHeight() {    var iframe = document.getElementById("frame_content");    var bHeight = iframe.contentWindow.document.body.scrollHeight;    var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;    alert("bHeight:" + bHeight + ", dHeight:" + dHeight);    }    </script>

 

被加载页面,可以切换一个绝对定位的层,来使页面高度动态改变。如果层展开,则会撑高页面高度。代码示例:

 


Js代码 复制代码
<div><button onclick="toggleOverlay()">Toggle Overlay</button>   
  
</div>   
  
<div style="height:160px;position:relative">   
  
<div id="overlay" style="position:absolute;width:280px;height:280px;display:none;"></div>   
  
</div>   
  
<script type="text/javascript">   
  
function toggleOverlay() {   
  
var overlay = document.getElementById('overlay');   
  
overlay.style.display = (overlay.style.display == 'none') ? 'block' : 'none';   
  
}   
  
</script>  
<div><button onclick="toggleOverlay()">Toggle Overlay</button>    </div>    <div style="height:160px;position:relative">    <div id="overlay" style="position:absolute;width:280px;height:280px;display:none;"></div>    </div>    <script type="text/javascript">    function toggleOverlay() {    var overlay = document.getElementById('overlay');    overlay.style.display = (overlay.style.display == 'none') ? 'block' : 'none';    }    </script>

 

下面列出以上代码在各浏览器的测试值:
(bHeight = body.scrollHeight, dHeight = documentElement.scrollHeight, 红色 = 错误值, 绿色 = 正确值)

 

 

暂且无视Opera比别人少3像素的问题…可以看出,如果没有绝对定位的东西,两个值是相等的,取哪个都无所谓。
但是如果有,那么各个浏览器的表现不太相同,单取哪个值都不对。但可以找到了一条规律,那就是取两个值得最大值可以兼容各浏览器。所以我们的主页面代码就要改造成这样了:

 


Js代码 复制代码
function reinitIframe(){var iframe = document.getElementById("frame_content");   
  
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()", 200);  
function reinitIframe(){var iframe = document.getElementById("frame_content");    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()", 200);

 

这样子,基本解决了兼容性问题。顺便说下,不光绝对定位的层会影响到值,float也会导致两个值的差异。

如果你演示Demo后,会发现,除了IE,其他浏览器中,当层展开后再隐藏,取到的高度值还是维持在展开的高度303,而非隐藏回去的真正值 184,就是说长高了之后缩不回去了。这个现象在不同被包含页面之间做切换也会发生,当从高的页面切换到矮页面的时候,取到的高度还是那个高的值。


可以归纳为,当iframe窗体高度高于文档实际高度的时候,高度取的是窗体高度,而当窗体高度低于实际文档高度时,取的是文档实际高度。因此,要想办法在同步高度之前把高度设置到一个比实际文档低的值。所以,在iframe的添加 onload=”this.height=100″,让页面加载的时候先缩到足够矮,然后再同步到一样的高度。
这个值,在实际应用中决定,足够矮但又不能太矮,否则在FF等浏览器里会有很明显的闪烁。DOM操作的时候主页面无法监听到,只能DOM操作完了之后把高度变小了。


在我的一个实际项目中,在成本和收益之间权衡,我并没有做这个事情,因为每个DOM函数中都要插入这个代码,代价太高,其实层缩回去不缩掉也不是那么致命。包括Demo里,也没有去做这个事情。如果读者有更好的方法,请告诉我。

这是最终的主页面的代码:

 


Js代码 复制代码
<iframe id="frame_content" src="iframe_b.html" scrolling="no" frameborder="0" onload="this.height=100"></iframe>   
  
<script type="text/javascript">   
  
function reinitIframe(){   
  
var iframe = document.getElementById("frame_content");   
  
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()", 200);   
  
</script>  
<iframe id="frame_content" src="iframe_b.html" scrolling="no" frameborder="0" onload="this.height=100"></iframe>    <script type="text/javascript">    function reinitIframe(){    var iframe = document.getElementById("frame_content");    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()", 200);    </script>

 

附Demo页面: 主页面 iframe_a.html ,被包含页面 iframe_b.htm 和 iframe_c.html

如果只考虑FX和IE,并且,iframe里面内容也不进行DOM操作,那仍然可以用最简单的传统处理方式:

 


Html代码 复制代码
<iframe id="frame_content" src="iframe_b.html" scrolling="no" frameborder="0" onload="this.height=this.contentWindow.document.documentElement.scrollHeight"></iframe>  
<iframe id="frame_content" src="iframe_b.html" scrolling="no" frameborder="0" onload="this.height=this.contentWindow.document.documentElement.scrollHeight"></iframe>  

 

以上转自:http://ued.koubei.com/?p=243

 

不考虑浏览器兼容性的情况下:

 


Html代码 复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>无标题文档</title>  
<script type="text/javascript">  
function tt(){   
      /*第一种:*/document.getElementById("main").style.height = document.frames["main"].document.body.scrollHeight+20;   
      /*第二种:*/document.getElementById("main").height= document.getElementById("main").contentWindow.document.documentElement.scrollHeight;   
      /*第三种:*/ document.getElementById("main").style.height=document.frames["main"].document.body.clientHeight+20;   
      //这里加20是因为document.body.clientHeight不同的浏览器值有所偏差,可以的话尽可能使用document.documentElement方式操作   
}   
</script>  
</head>  
<body >  
<iframe src="Untitled-1.html" width="100%"  frameborder="0" scrolling="no" id="main"  onload="tt()">  
</iframe>  
</body>  
</html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">  <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title>无标题文档</title>  <script type="text/javascript">  function tt(){        /*第一种:*/document.getElementById("main").style.height = document.frames["main"].document.body.scrollHeight+20;     /*第二种:*/document.getElementById("main").height= document.getElementById("main").contentWindow.document.documentElement.scrollHeight;     /*第三种:*/ document.getElementById("main").style.height=document.frames["main"].document.body.clientHeight+20;     //这里加20是因为document.body.clientHeight不同的浏览器值有所偏差,可以的话尽可能使用document.documentElement方式操作  }  </script>  </head>  <body >  <iframe src="Untitled-1.html" width="100%"  frameborder="0" scrolling="no" id="main"  onload="tt()">  </iframe>  </body>  </html>  

 

 

只支持IE:

(1).1、建立一个bottom.js的文件,然后输入下面的代码(只有两行哦) 
parent.document.all("框架ID名").style.height=document.body.scrollHeight; 
parent.document.all("框架ID名").style.width=document.body.scrollWidth; 
这里的 框架ID名 就是Iframe的ID,比如: 
<IFRAME id="框架ID名" name="left" frameBorder=0 scrolling=no src="XXX.aspx" width="100%"> </IFRAME> 
2、给你网站里所有的被包含文件里面每个都加入 
<script language = "JavaScript" src = "bottom.js"/> </script> 
3、OK,收工! 
我在WIN2003、IE6下面测试通过。很简单吧!

 


Html代码 复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>无标题文档</title>  
<script type="text/javascript">  
function SetcwinHeight(){   
        //var ifrHeight=document.getElementById("main");   
        /*if(document.getElementById)代表:如果浏览器支持getElementById,判断成立,否则相当于if(undefined),判断不成立。   
        多用于检测浏览器是否支持某项特征,注意,一定不能写成if(document.getElementById())   
        加上括号会有返回值。*/   
        /*   
        document.all 只有IE支持   
        document.layers 只有Netscape支持   
        document.getElementById 是符合W3C标准   
        */   
           
      var bobo=document.getElementById("main"); //iframe id   
      if (document.getElementById){   
       if (bobo && !window.opera){   
        if (bobo.contentDocument && bobo.contentDocument.body.offsetHeight){   
         bobobobo.height = bobo.contentDocument.body.offsetHeight;<!--经测试火狐执行这段代码!-->  
        }else if(bobo.Document && bobo.Document.body.scrollHeight){   
         bobobobo.height = bobo.Document.body.scrollHeight+30;<!--IE执行该代码-->  
         alert(bobo.Document.body.scrollHeight+30);   
        }   
       }   
      }   
        
  
}   
</script>  
</head>  
  
<body>  
<iframe src="Untitled-4.html" width="100%" id="main"   scrolling="no" frameborder="0" onload="SetcwinHeight();">  
</body>  
</html>
0 0
原创粉丝点击