欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)iframe自適應(yīng)高度的方法示例

 更新時(shí)間:2021年08月22日 23:28:24   作者:pyooer  
這篇文章主要介紹了JS實(shí)現(xiàn)iframe自適應(yīng)高度的方法,結(jié)合實(shí)例形式分析了JS實(shí)現(xiàn)iframe高度自適應(yīng)的實(shí)現(xiàn)技巧,并給出了項(xiàng)目示例供大家參考,需要的朋友可以參考下

本文實(shí)例講述了JS實(shí)現(xiàn)iframe自適應(yīng)高度的方法。

JS自適應(yīng)高度,其實(shí)就是設(shè)置iframe的高度,使其等于內(nèi)嵌網(wǎng)頁(yè)的高度,從而看不出來(lái)滾動(dòng)條和嵌套痕跡。對(duì)于用戶體驗(yàn)和網(wǎng)站美觀起著重要作用。 如果內(nèi)容是固定的,那么我們可以通過(guò)CSS來(lái)給它直接定義一個(gè)高度,同樣可以實(shí)現(xiàn)上面的需求。當(dāng)內(nèi)容是未知或者是變化的時(shí)候。這個(gè)時(shí)候又有幾種情況了。

iframe內(nèi)容未知,高度可預(yù)測(cè)

這個(gè)時(shí)候,我們可以給它添加一個(gè)默認(rèn)的CSS的min-height值,然后同時(shí)使用JavaScript改變高度。常用的兼容代碼有: 

// document.domain = "caibaojian.com";
function setIframeHeight(iframe) {
if (iframe) {
var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
if (iframeWin.document.body) {
iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
}
}
};

window.onload = function () {
setIframeHeight(document.getElementById('external-frame'));
};

 演示地址

<iframe src="backtop.html" frameborder="0" scrolling="no" id="external-frame" onload="setIframeHeight(this)"></iframe>

 演示二

 多個(gè)iframe的情況下 

<script language="javascript">
//輸入你希望根據(jù)頁(yè)面高度自動(dòng)調(diào)整高度的iframe的名稱的列表
//用逗號(hào)把每個(gè)iframe的ID分隔. 例如: ["myframe1", "myframe2"],可以只有一個(gè)窗體,則不用逗號(hào)。
//定義iframe的ID
var iframeids=["test"];
//如果用戶的瀏覽器不支持iframe是否將iframe隱藏 yes 表示隱藏,no表示不隱藏
var iframehide="yes";
function dyniframesize()
{
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++)
{
if (document.getElementById)
{
//自動(dòng)調(diào)整iframe高度
dyniframe[dyniframe.length] = document.getElementById(iframeids[i]);
if (dyniframe[i] && !window.opera)
{
dyniframe[i].style.display="block";
if (dyniframe[i].contentDocument && dyniframe[i].contentDocument.body.offsetHeight) //如果用戶的瀏覽器是NetScape
dyniframe[i].height = dyniframe[i].contentDocument.body.offsetHeight;
else if (dyniframe[i].Document && dyniframe[i].Document.body.scrollHeight) //如果用戶的瀏覽器是IE
dyniframe[i].height = dyniframe[i].Document.body.scrollHeight;
}
}
//根據(jù)設(shè)定的參數(shù)來(lái)處理不支持iframe的瀏覽器的顯示問(wèn)題
if ((document.all || document.getElementById) && iframehide=="no")
{
var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i]);
tempobj.style.display="block";
}
}
}
if (window.addEventListener)
window.addEventListener("load", dyniframesize, false);
else if (window.attachEvent)
window.attachEvent("onload", dyniframesize);
else
window.onload=dyniframesize;
</script>

 演示三

針對(duì)知道的iframe的ID調(diào)用

function iframeAutoFit(iframeObj){
setTimeout(function(){if(!iframeObj) return;iframeObj.height=(iframeObj.Document?iframeObj.Document.body.scrollHeight:iframeObj.contentDocument.body.offsetHeight);},200)
}

 演示四

內(nèi)容寬度變化的iframe高度自適應(yīng)

<iframe src="backtop.html" frameborder="0" scrolling="no" id="test" onload="this.height=100"></iframe>
<script type="text/javascript">
function reinitIframe(){
var iframe = document.getElementById("test");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
console.log(height);
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);
</script>

演示五 打開(kāi)調(diào)試運(yùn)行窗口可以看到運(yùn)行。

跨域下的iframe自適應(yīng)高度

跨域的時(shí)候,由于js的同源策略,父頁(yè)面內(nèi)的js不能獲取到iframe頁(yè)面的高度。需要一個(gè)頁(yè)面來(lái)做代理。 方法如下:假設(shè)www.a.com下的一個(gè)頁(yè)面a.html要包含www.b.com下的一個(gè)頁(yè)面c.html。 我們使用www.a.com下的另一個(gè)頁(yè)面agent.html來(lái)做代理,通過(guò)它獲取iframe頁(yè)面的高度,并設(shè)定iframe元素的高度。 a.html中包含iframe:

<iframe src="http://www.b.com/c.html" id="Iframe" frameborder="0" scrolling="no" style="border:0px;"></iframe>

在c.html中加入如下代碼:

<iframe id="c_iframe"  height="0" width="0"  src="http://www.a.com/agent.html" style="display:none" ></iframe>
<script type="text/javascript">
(function autoHeight(){
var b_width = Math.max(document.body.scrollWidth,document.body.clientWidth);
var b_height = Math.max(document.body.scrollHeight,document.body.clientHeight);
var c_iframe = document.getElementById("c_iframe");
c_iframe.src = c_iframe.src + "#" + b_width + "|" + b_height;  // 這里通過(guò)hash傳遞b.htm的寬高
})();
</script>

最后,agent.html中放入一段js:

<script type="text/javascript">
var b_iframe = window.parent.parent.document.getElementById("Iframe");
var hash_url = window.location.hash;
if(hash_url.indexOf("#")>=0){
var hash_width = hash_url.split("#")[1].split("|")[0]+"px";
var hash_height = hash_url.split("#")[1].split("|")[1]+"px";
b_iframe.style.width = hash_width;
b_iframe.style.height = hash_height;
}
</script>

 agent.html從URL中獲得寬度值和高度值,并設(shè)置iframe的高度和寬度(因?yàn)閍gent.html在www.a.com下,所以操作a.html時(shí)不受JavaScript的同源限制) 演示六 

分享給大家供大家參考,具體如下:

<iframe id="mainFrame" name="mainFrame" src="/zwgk/hsearchview" width="740" frameborder="0" scrolling="no" scrolling="no" frameborder="0" >
</iframe>

<script type="text/javascript" language="javascript">
function reinitIframe(){
 var iframe = document.getElementById("mainFrame");
 try{
 var bHeight = iframe.contentWindow.document.body.scrollHeight;
 var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
 var height = Math.max(bHeight, dHeight);
 iframe.height = height;
 }catch (ex){}
 }
 window.setInterval("reinitIframe()", 100);
</script>

PS:高度自適應(yīng)應(yīng)用廣泛,本站的很多在線工具也使用了這一技巧,列舉如下幾個(gè)工具供大家參考:

JavaScript在線格式化工具(基于beautify.js插件):
http://tools.jb51.net/code/js_beautify

在線顏色選擇器工具/RGB顏色查詢對(duì)照表:
http://tools.jb51.net/color/colorpicker

在線個(gè)人所得稅計(jì)算器:
http://tools.jb51.net/jisuanqi/tax_calc

宋詞在線查詢:
http://tools.jb51.net/bianmin/songci

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript排序算法總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論