手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >让iframe自适应高度(支持XHTML,支持FF)
让iframe自适应高度(支持XHTML,支持FF)
摘要:先说明,这个办法只限于iframe中的子页面也是本地页面(不能引用外网页面)======方法=====第一步js部分functiongetS...

先说明,这个办法只限于iframe中的子页面也是本地页面(不能引用外网页面)

======方法=====

第一步js部分

functiongetSize(){

varxScroll,yScroll;

if(window.innerHeight&&window.scrollMaxY){

xScroll=document.body.scrollWidth;

yScroll=window.innerHeight+window.scrollMaxY;

}elseif(document.body.scrollHeight>document.body.offsetHeight){//allbutExplorerMac

xScroll=document.body.scrollWidth;

yScroll=document.body.scrollHeight;

}else{//ExplorerMac...wouldalsoworkinExplorer6Strict,MozillaandSafari

xScroll=document.body.offsetWidth;

yScroll=document.body.offsetHeight;

}

varwindowWidth,windowHeight;

if(self.innerHeight){//allexceptExplorer

windowWidth=self.innerWidth;

windowHeight=self.innerHeight;

}elseif(document.documentElement&&document.documentElement.clientHeight){//Explorer6StrictMode

windowWidth=document.documentElement.clientWidth;

windowHeight=document.documentElement.clientHeight;

}elseif(document.body){//otherExplorers

windowWidth=document.body.clientWidth;

windowHeight=document.body.clientHeight;

}

//forsmallpageswithtotalheightlessthenheightoftheviewport

if(yScroll<windowHeight){

pageHeight=windowHeight;

y=pageHeight;

}else{

pageHeight=yScroll;

y=pageHeight;

}

if(xScroll<windowWidth){

pageWidth=windowWidth;

}else{

pageWidth=xScroll;

}

arrayPageSize=newArray(pageWidth,pageHeight,windowWidth,windowHeight)

returnarrayPageSize;

}

这段代码用来获取目标页的参数,包括页面高、宽,屏幕高、宽

functionautoHeight(pid){

varx=newgetSize();

parent.document.getElementById(pid).height=x[1];

}

这段代码用来实现父页面中iframe的高度自适应

第二步页面部分

<divclass="onright"style="width:480px;"><iframeid="infrm"name="infrm"marginwidth="0"marginheight="0"width="100%"src="park.htm"frameborder="0"scrolling="auto"></iframe>

这是父页面的iframe,没什么特别的,和普通的iframe一样,不过要设置好id值,以便子页面的参数调用。

<bodyonload="autoHeight('infrm')"></body>

在body中利用onload事件,将自身的高度传给父页面的iframe。

【让iframe自适应高度(支持XHTML,支持FF)】相关文章:

jQuery实现在列表的首行添加数据

探索angularjs+requirejs全面实现按需加载的套路

prototype 1.5 & scriptaculous 1.6.1 学习笔记

破解Session cookie的方法

JavaScript实现Iterator模式实例分析

非常酷的有农历的日历挂历!

关于IFRAME 自适应高度的研究

JavaScript通过事件代理高亮显示表格行的方法

简述JavaScript中正则表达式的使用方法

使用Node.js处理前端代码文件的编码问题

精品推荐
分类导航