用iframe自动适应高度

很多做的人都用过IFrame,也常希望能让IFrame自动改变大小,以美化全局效果。以下是两种方法
方法一:(较通用)
<script language="Javascript">
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0

function dyniframesize(iframename) {
var pTar = null;
if (document.getElementById){
pTar = document.getElementById(iframename);
}
else{
eval('pTar = ' + iframename + ';');
}
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+FFextraHeight;
}
else if (pTar.Document && pTar.Document.body.scrollHeight){
//ie5+ syntax
pTar.height = pTar.Document.body.scrollHeight;
}
}
}
</script>

<iframe id="myTestFrameID"
οnlοad="javascript:{dyniframesize('myTestFrameID');}"
marginwidth=0 marginheight=0 frameborder=0
scrolling=no src="/myiframesrc.php"
width=200 height=100></iframe><script language="Javascript">
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0

function dyniframesize(iframename) {
var pTar = null;
if (document.getElementById){
pTar = document.getElementById(iframename);
}
else{
eval('pTar = ' + iframename + ';');
}
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+FFextraHeight;
}
else if (pTar.Document && pTar.Document.body.scrollHeight){
//ie5+ syntax
pTar.height = pTar.Document.body.scrollHeight;
}
}
}
</script>

<iframe id="myTestFrameID"
οnlοad="javascript:{dyniframesize('myTestFrameID');}"
marginwidth=0 marginheight=0 frameborder=0
scrolling=no src="/myiframesrc.php"
width=200 height=100></iframe>
方法二:(较简便)
<IFRAME id=headlogin marginWidth=0 marginHeight=0 src="xxx" frameBorder=0 width=100% scrolling=no height=25 οnlοad="this.height=this.contentWindow.document.body.scrollHeight"></IFRAME>

 

 

 

 

 

项目中具体应用


// JavaScript Document
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0
var FFextraWidth=getFFVersion>=0.1? 16 : 0

function dyniframesize(iframename) {
var pTar = null;
if (document.getElementById){
    pTar = document.getElementById(iframename);
}
else{
    eval('pTar = ' + iframename + ';');
}
if (pTar){
    //begin resizing iframe
 //delete  && !window.opera jenny 090720
    pTar.style.display="block";
   
    if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
      //ns6 syntax
      pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight+5+'px';
   pTar.width =pTar.contentDocument.body.offsetWidth+FFextraWidth+5+'px';
    }
    else if (pTar.Document && pTar.Document.body.scrollHeight){
      //ie5+ syntax
      pTar.height = pTar.Document.body.scrollHeight+5+'px';
   pTar.width = pTar.Document.body.scrollWidth+5+'px';
    }
 
}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现iframe自动适应高度,可以通过以下步骤进行操作,以保证兼容主流浏览器。 首先,需要在iframe的源文件中添加一段脚本代码来实现自适应高度的功能。具体代码如下所示: ```html <script type="text/javascript"> function autoResize() { var iframe = parent.document.getElementById("your-iframe-id"); if (iframe) { iframe.style.height = iframe.contentWindow.document.body.scrollHeight + 'px'; } } window.onload = autoResize; </script> ``` 上述代码中的"your-iframe-id"需要替换为你实际的iframe元素的id。 然后,在iframe的父页面中,需要通过以下代码来监听iframe内容的变化,并触发自适应高度的函数: ```html <script type="text/javascript"> function setIframeHeight() { var iframe = document.getElementById("your-iframe-id"); iframe.onload = function() { this.style.height = this.contentWindow.document.body.scrollHeight + 'px'; }; } window.onload = setIframeHeight; </script> ``` 同样,上述代码中的"your-iframe-id"需要替换为实际的iframe元素的id。 这样,当iframe的内容发生变化时,它的高度自动适应内容的高度。 最后,在需要插入iframe的地方,添加以下代码即可: ```html <iframe id="your-iframe-id" src="your-source-url"></iframe> ``` 其中,"your-iframe-id"为iframe元素的id,"your-source-url"为iframe显示的源文件的URL。 以上操作可以实现iframe的自适应高度,并兼容目前主流浏览器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值