您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

Iframe 自适应高度并实时监控高度变化的js代码

2024/4/25 3:37:41发布4次查看
google n次 + 百度m次 + 试验了1605次之后(听说农药1605就是实验了这么多次后出来的),得出下面成果,在ie7及firefox3里试了下还能凑合着用用!
1、首先给出个iframe。
<iframe id="ifrm" marginheight="0" marginwidth="0" height="100" width="1000" frameborder="0" scrolling="no" src="xxxxx.html"> </iframe>
2、然后看看怎么获取iframe中的页面的高度。
其实最麻烦的就是怎么让获取的高度准确,不同的方式不同的浏览器中获取到的值都会不一样!晕啊~~。参考了多方意见得出以下javascript函数(doc参数为window.document对象):
function getdocheight(doc) { //在ie中doc.body.scrollheight的可信度最高 //在firefox中,doc.height就可以了 var dochei = 0; var scrollhei;//scrollheight var offsethei;//offsetheight,包含了边框的高度 if (doc.height) { //firefox支持此属性,ie不支持 dochei = doc.height; } else if (doc.body) { //在ie中,只有body.scrollheight是与当前页面的高度一致的, //其他的跳转几次后就会变的混乱,不知道是依照什么取的值! //似乎跟包含它的窗口的大小变化有关 if(doc.body.offsetheight) dochei = offsethei = doc.body.offsetheight; if(doc.body.scrollheight) dochei = scrollhei = doc.body.scrollheight; } else if(doc.documentelement) { if(doc.documentelement.offsetheight) dochei = offsethei = doc.documentelement.offsetheight; if(doc.documentelement.scrollheight) dochei = scrollhei = doc.documentelement.scrollheight; } /* dochei = math.max(scrollhei,offsethei);//取最大的值,某些情况下可能与实际页面高度不符! */ return dochei; }
3、最后修改iframe的高度,并用一个定时器来不间断检查它包含的页面的高度变化。
function doresize() { var iframewin = window.frames['ifrm']; var iframeel = window.document.getelementbyid? window.document.getelementbyid('ifrm'): document.all? document.all['ifrm']: null; if ( iframeel && iframewin ) { var dochei = getdocheight(iframewin.document); if (dochei != iframeel.style.height) iframeel.style.height = dochei + 'px'; } else if(iframeel) { var dochei = getdocheight(iframeel.contentdocument); if (dochei != iframeel.style.height) iframeel.style.height = dochei + 'px'; } } function runresizetask() { doresize(); settimeout("runresizetask()",500);//每隔半秒执行一次 } runresizetask();
在这里就没有去考虑被包含的页面中是否有折叠、隐藏/展现的情况了!
完整js代码
<script language="javascript" type="text/javascript"> function getdocheight(doc){ //在ie中doc.body.scrollheight的可信度最高 //在firefox中,doc.height就可以了 var dochei = 0; var scrollhei;//scrollheight var offsethei;//offsetheight,包含了边框的高度 if (doc.height){ //firefox支持此属性,ie不支持 dochei = doc.height; } else if (doc.body){ //在ie中,只有body.scrollheight是与当前页面的高度一致的, //其他的跳转几次后就会变的混乱,不知道是依照什么取的值! //似乎跟包含它的窗口的大小变化有关 if(doc.body.offsetheight) dochei = offsethei = doc.body.offsetheight; if(doc.body.scrollheight) dochei = scrollhei = doc.body.scrollheight; } else if(doc.documentelement){ if(doc.documentelement.offsetheight) dochei = offsethei = doc.documentelement.offsetheight; if(doc.documentelement.scrollheight) dochei = scrollhei = doc.documentelement.scrollheight; } /* dochei = math.max(scrollhei,offsethei);//取最大的值,某些情况下可能与实际页面高度不符! */ return dochei; } function doresize(){ var iframewin = window.frames['main']; var iframeel = window.document.getelementbyid? window.document.getelementbyid('main'): document.all? document.all['main']: null; if ( iframeel && iframewin ){ var dochei = getdocheight(iframewin.document); if (dochei != iframeel.style.height) iframeel.style.height = dochei + 'px'; } else if(iframeel){ var dochei = getdocheight(iframeel.contentdocument); if (dochei != iframeel.style.height) iframeel.style.height = dochei + 'px'; } } function runresizetask(){ doresize(); settimeout("runresizetask()",1000);//每隔1秒执行一次 } runresizetask(); </script>
更多iframe 自适应高度并实时监控高度变化的js代码。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录