让iframe高度自适应

来源:互联网 发布:淘宝的插件管理在哪里 编辑:程序博客网 时间:2024/05/01 12:06

总结两种方法,第一种是在网上查资料然后总结出来的,兼容性比较好,支持IE8,以及其他的标准浏览器,IE8以下版本没有测试,IE8以上版本基本都支持,这种方法是通过js来调整iframe的宽高的,第二种方法很简单,是我看朋友是这样的写的,具体兼容性没有测试,应该不会差。不多少了,直接贴代码:

第一种:

  <script type="text/javascript"> 
  function findDimensions(){  //函数:获取尺寸
  var winWidth = 0; 
var winHeight = 0; 
//获取窗口宽度 
if (window.innerWidth){ 
winWidth = window.innerWidth; 
}else if ((document.body) && (document.body.clientWidth)){ 
winWidth = document.body.clientWidth;
}
//获取窗口高度 
if (window.innerHeight){  
winHeight = window.innerHeight; 
}else if ((document.body) && (document.body.clientHeight)){ 
winHeight = document.body.clientHeight;
}
//通过深入Document内部对body进行检测,获取窗口大小 
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth){ 
winHeight = document.documentElement.clientHeight; 
winWidth = document.documentElement.clientWidth; 

//调整div的高度以及iframe的宽高 
var iframehei = winHeight - 180;
var iframeWid = winWidth - 310;
document.getElementById("Conframe").style.height=iframehei+"px";
document.getElementById("Conframe").style.width=iframeWid+"px";

  
  $(document).ready(function(){
findDimensions();
menuOnClick("homePage/homePage.do"); 
});
//调用函数,获取数值 
$(window).resize(function(){
findDimensions();
});
  </script>


第二种:

 <iframe name="main" src="home.jsp" width=100% onload="this.height=this.contentWindow.document.body.scrollHeight" frameborder=No border=0 marginwidth=0 marginheight=0 scrolling=no></iframe>(第二种是不是很简单?直接这样就行了
onload="this.height=this.contentWindow.document.body.scrollHeight"


1 0
原创粉丝点击