自动适应iframe右边的高度
在开发项目过程中,用iframe嵌套,会发现一个问题,用iframe嵌套的html结构右边不会自动适应高度。
这时候找到了一个解决方法:
<iframename="my_iframe"id="mainframe"marginheight="0"marginwidth="0"frameborder="0"scrolling="no"width="100%"height="100%"src=""></iframe>
2、记住要引入iframe.js文件
<scripttype="text/javascript"src="js/iframe.js"></script>
下面是iframe.js的具体内容
varbrowserVersion=window.navigator.userAgent.toUpperCase();
varisOpera=browserVersion.indexOf("OPERA")>-1?true:false;
varisFireFox=browserVersion.indexOf("FIREFOX")>-1?true:false;
varisChrome=browserVersion.indexOf("CHROME")>-1?true:false;
varisSafari=browserVersion.indexOf("SAFARI")>-1?true:false;
varisIE=(!!window.ActiveXObject||"ActiveXObject"inwindow);
varisIE9More=(!-[1,]==false);
functionreinitIframe(iframeId,minHeight){
try{
variframe=document.getElementById(iframeId);
varbHeight=0;
if(isChrome==false&&isSafari==false)
bHeight=iframe.contentWindow.document.body.scrollHeight;
vardHeight=0;
if(isFireFox==true)
dHeight=iframe.contentWindow.document.documentElement.offsetHeight+2;
elseif(isIE==false&&isOpera==false)
dHeight=iframe.contentWindow.document.documentElement.scrollHeight;
elseif(isIE==true&&isIE9More){//ie9+
varheightDeviation=bHeight-eval("window.IE9MoreRealHeight"+iframeId);
if(heightDeviation==0){
bHeight+=3;
}elseif(heightDeviation!=3){
eval("window.IE9MoreRealHeight"+iframeId+"="+bHeight);
bHeight+=3;
}
}
else//ie[6-8]、OPERA
bHeight+=3;
varheight=Math.max(bHeight,dHeight);
if(height<minHeight)height=minHeight;
iframe.style.height=height+"px";
}catch(ex){}
}
functionstartInit(iframeId,minHeight){
eval("window.IE9MoreRealHeight"+iframeId+"=0");
window.setInterval("reinitIframe('"+iframeId+"',"+minHeight+")",100);
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!