手机
当前位置:查字典教程网 >编程开发 >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)】相关文章:

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

实现placeholder效果的方案汇总

滚动效果

JavaScript中Cookies的相关使用教程

日期函数扩展类Ver0.1.1

Js的Array数组对象详解

JavaScript中的toLocaleLowerCase()方法使用详解

JS实现兼容各浏览器解析XML文档数据的方法

破解Session cookie的方法

JavaScript编程学习技巧汇总

精品推荐
分类导航