让Iframe自适应高度

来源:互联网 发布:恩施有没有淘宝网 编辑:程序博客网 时间:2024/05/01 20:03

让Iframe自适应高度
iframe,尤其是不带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能,可是 iframe的大小却不像层那样可以“伸缩自如”,所以带来了使用上的麻烦,给iframe设置高度的时候多了也不好,少了更是不行,现在,让我来告诉大 家一种iframe动态调整高度的方法,主要是以下JS函数:
程序代码
function SetWinHeight(obj)
{
var win=obj;
if (document.getElementById)
{
if (win && !window.opera)
{
   if (win.contentDocument && win.contentDocument.body.offsetHeight)
     win.height = win.contentDocument.body.offsetHeight;
   else if(win.Document && win.Document.body.scrollHeight)
     win.height = win.Document.body.scrollHeight;
}
}
}
最后,加入iframe,不能丢掉onload属性,当然了,id也必须也函数中的win匹配
程序代码
<iframe width="778" align="center" height="200" id="win" name="win"   frameborder="0" scrolling="no" onload="Javascript:SetWinHeight(this)"></iframe>
另一种情况的iframe解决方案(超简单)
重要提示:src=中你必须填写的网页地址,一定要和本页面在同一个站点上,否则,会抱错,说“拒绝访问!”(实际上这是因为Js的跨域问题导致拒绝访问的)
之前自己也碰到过这个问题,为了得到答案去网上搜索,发现有不少人也遇到了这样的问题,现在就把解决方法共享一下
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.asp" width="100%"></IFRAME>
2、给你网站里所有的被包含文件里面每个都加入
<script language = "JavaScript" src = "bottom.js"/></script>
3、OK,收工!
在WINXP、IE6下面测试通过。很简单吧!
实现 iframe 的自适应高度
实现 iframe 的自适应高度,能够随着页面的长度自动的适应以免除页面和 iframe 同时出现滚动条的现象。
程序代码
<script type="text/javascript">
//** iframe自动适应页面 **//
//输入你希望根据页面高度自动调整高度的iframe的名称的列表
//用逗号把每个iframe的ID分隔. 例如: ["myframe1", "myframe2"],可以只有一个窗体,则不用逗号。
//定义iframe的ID
var iframeids=["test"]
//如果用户的浏览器不支持iframe是否将iframe隐藏 yes 表示隐藏,no表示不隐藏
var iframehide="yes"
function dyniframesize()
{
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++)
{
if (document.getElementById)
{
//自动调整iframe高度
dyniframe[dyniframe.length] = document.getElementById(iframeids);
if (dyniframe && !window.opera)
{
dyniframe.style.display="block"
if (dyniframe.contentDocument && dyniframe.contentDocument.body.offsetHeight) //如果用户的浏览器是NetScape
dyniframe.height = dyniframe.contentDocument.body.offsetHeight;
else if (dyniframe.Document && dyniframe.Document.body.scrollHeight) //如果用户的浏览器是IE
dyniframe.height = dyniframe.Document.body.scrollHeight;
}
}
//根据设定的参数来处理不支持iframe的浏览器的显示问题
if ((document.all || document.getElementById) && iframehide=="no")
{
var tempobj=document.all? document.all[iframeids] : document.getElementById(iframeids)
tempobj.style.display="block"
}
}
}
if (window.addEventListener)
window.addEventListener("load", dyniframesize, false)
else if (window.attachEvent)
window.attachEvent("onload", dyniframesize)
else
window.
</script>
实际例子:
<!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=gb2312" />
<title></title>
</head>
<link href="style/base.css" rel="stylesheet" type="text/css"/>
<script >
function SetCwinHeight(){
var mright=document.getElementById("mright"); //iframe id
if (document.getElementById){
   if (mright && !window.opera){
    if (mright.contentDocument && mright.contentDocument.body.offsetHeight){
     mright.height = mright.contentDocument.body.offsetHeight;
    }else if(mright.Document && mright.Document.body.scrollHeight){
     mright.height = mright.Document.body.scrollHeight;
    }
   }
}
var mleft=document.getElementById("mleft"); //iframe id
if (document.getElementById){
   if (mleft && !window.opera){
    if (mleft.contentDocument && mleft.contentDocument.body.offsetHeight){
     mleft.height = mleft.contentDocument.body.offsetHeight;
    }else if(mleft.Document && mleft.Document.body.scrollHeight){
     mleft.height = mleft.Document.body.scrollHeight;
    }
   }
}
}
</script>
<body>
<div id="main">
<iframe name="mleft" onload="Javascript:SetCwinHeight()" align="top" width="197" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" src="zc_left.htm"></iframe>
<iframe name="mright" align="top" width="580" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" src="zc_right.htm"></iframe>
</div>
</body>
</html>

原创粉丝点击