Iframe自适应高度

来源:互联网 发布:cc攻击防御软件 编辑:程序博客网 时间:2024/05/04 18:35

第一种:

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 width="778" align="center" height="200" id="win" name="win" onload="Javascript:SetWinHeight(this)"frameborder="0" scrolling="no" src="1.htm"></iframe>


第二种:IE6/IE7/IE8/Firefox/Opera/Chrome/Safari

<iframe src="http://www.fufuok.com/" id="iframepage" name="iframepage" frameBorder=0 scrolling=nowidth="100%" onLoad="iFrameHeight()" ></iframe>

 
<script type="text/javascript" language="javascript">
function iFrameHeight() {
var ifm= document.getElementById("iframepage");
var subWeb = document.frames ? document.frames["iframepage"].document :ifm.contentDocument;
if(ifm != null && subWeb != null)

 {
ifm.height = subWeb.body.scrollHeight;
}
}
</script> 


第三种:兼容性更好一些


<script language="javascript" type="text/javascript">
function dyniframesize(down) {
var pTar = null;
if (document.getElementById)
{
pTar = document.getElementById(down);
}
else{
eval('pTar = ' + down + ';');
}
if (pTar && !window.opera)
{
//begin resizing iframe
pTar.style.display="block"
if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
//ns6 syntax
pTar.height = pTar.contentDocument.body.offsetHeight +20;
pTar.width = pTar.contentDocument.body.scrollWidth+20;
}
else if (pTar.Document && pTar.Document.body.scrollHeight){
//ie5+ syntax
pTar.height = pTar.Document.body.scrollHeight;
pTar.width = pTar.Document.body.scrollWidth;
}
}
}
</script>
<iframe src ="/default2.aspx" frameborder="0" marginheight="0" marginwidth="0"
frameborder="0" scrolling="auto" id="ifm" name="ifm" onload="javascript:dyniframesize('ifm');" width="100%">
</iframe> 



第四种:

function iframeAutoFit(iframeObj){
     setTimeout(function(){if(!iframeObj) return;iframeObj.height=(iframeObj.Document? iframeObj.Document.body.scrollHeight:iframeObj.contentDocument.body.offsetHeight);},200)
}



原文连接:http://www.jb51.net/article/15780.htm
























































































0 0
原创粉丝点击