让iframe高度自适应

总结两种方法。第一种是在网上查资料然后总结出来的,兼容性比較好,支持IE8,以及其它的标准浏览器。IE8下面版本号没有測试。IE8以上版本号基本都支持,这个方案是通过js来调整iframe的宽高的。另外一种方法非常easy。是我看朋友是这种写的,详细兼容性没有測试,应该不会差。不多少了,直接贴代码:

第一种:

  <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>(另外一种是不是非常easy?直接这样即可了
onload="this.height=this.contentWindow.document.body.scrollHeight"


posted on 2017-06-22 16:50  wgwyanfs  阅读(119)  评论(0编辑  收藏  举报

导航