|
======方法=====
第一步 js部分
function getSize() {
var xScroll, yScroll;
if (window.innerHeight && window.scrollMaxY) {
xScroll = document.body.scrollWidth;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
var windowWidth, windowHeight;
if (self.innerHeight) { // all except Explorer
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}
// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){
pageHeight = windowHeight;
y = pageHeight;
} else {
pageHeight = yScroll;
y = pageHeight;
}
if(xScroll < windowWidth){
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
return arrayPageSize;
}
這段代碼用來(lái)獲取目標(biāo)頁(yè)的參數(shù),包括頁(yè)面高、寬,屏幕高、寬
function autoHeight(pid) {
var x = new getSize();
parent.document.getElementById(pid).height=x[1];
}
這段代碼用來(lái)實(shí)現(xiàn)父頁(yè)面中iframe的高度自適應(yīng)
//////////
第二步 頁(yè)面部分
<div class="onright" style="width:480px;"><iframe id="infrm" name="infrm" marginwidth="0" marginheight="0" width="100%" src="park.htm" frameborder="0" scrolling="auto"></iframe>
這是父頁(yè)面的iframe,沒(méi)什么特別的,和普通的iframe一樣,不過(guò)要設(shè)置好id值,以便子頁(yè)面的參數(shù)調(diào)用。
<body onload="autoHeight('infrm')"></body>
在body中利用onload事件,將自身的高度傳給父頁(yè)面的iframe。
JavaScript技術(shù):讓iframe自適應(yīng)高度(支持XHTML,支持FF),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。