用javascript動(dòng)態(tài)調(diào)整iframe高度的方法
更新時(shí)間:2007年03月06日 00:00:00 作者:
當(dāng)你在頁(yè)面上使用了iframe之后,一般來(lái)說(shuō)會(huì)不希望iframe顯示難看的滾動(dòng)條,以使iframe里面的內(nèi)容和主頁(yè)面的內(nèi)容渾然一體。這時(shí)候你會(huì)設(shè)置 scrolling="no" 屬性。但是這樣一來(lái)如果iframe里面的內(nèi)容是變化的,高度會(huì)隨之內(nèi)容的變化而變化的時(shí)候,你的iframe就會(huì)顯得太長(zhǎng)導(dǎo)致底下一大片空白,或者正好相反,由于iframe的高度太小導(dǎo)致一部分內(nèi)容會(huì)被擋住。這里我提供一個(gè)兼容IE/NS/Firefox的javascript腳本實(shí)現(xiàn)動(dòng)態(tài)調(diào)整iframe的高度。如果需要調(diào)整寬度的話(huà),原理是一樣的,本文不加詳述。
首先,在你的主頁(yè)面上必須包含以下這段javascript代碼:
<script language="Javascript">
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0
function dyniframesize(iframename) {
var pTar = null;
if (document.getElementById){
pTar = document.getElementById(iframename);
}
else{
eval('pTar = ' + iframename + ';');
}
if (pTar && !window.opera){
//begin resizing iframe
pTar.style.display="block"
if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
//ns6 syntax
pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight;
}
else if (pTar.Document && pTar.Document.body.scrollHeight){
//ie5+ syntax
pTar.height = pTar.Document.body.scrollHeight;
}
}
}
</script>
然后對(duì)于主頁(yè)面用到iframe的地方添加代碼:
<iframe id="myTestFrameID"
onload="javascript:{dyniframesize('myTestFrameID');}"
marginwidth=0 marginheight=0 frameborder=0
scrolling=no src="/myiframesrc.php"
首先,在你的主頁(yè)面上必須包含以下這段javascript代碼:
復(fù)制代碼 代碼如下:
<script language="Javascript">
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0
function dyniframesize(iframename) {
var pTar = null;
if (document.getElementById){
pTar = document.getElementById(iframename);
}
else{
eval('pTar = ' + iframename + ';');
}
if (pTar && !window.opera){
//begin resizing iframe
pTar.style.display="block"
if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
//ns6 syntax
pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight;
}
else if (pTar.Document && pTar.Document.body.scrollHeight){
//ie5+ syntax
pTar.height = pTar.Document.body.scrollHeight;
}
}
}
</script>
然后對(duì)于主頁(yè)面用到iframe的地方添加代碼:
<iframe id="myTestFrameID"
onload="javascript:{dyniframesize('myTestFrameID');}"
marginwidth=0 marginheight=0 frameborder=0
scrolling=no src="/myiframesrc.php"
相關(guān)文章
JavaScript實(shí)現(xiàn)打字效果的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)打字效果的方法,可實(shí)現(xiàn)文字陸續(xù)出現(xiàn)的打字效果,涉及javascript時(shí)間函數(shù)及頁(yè)面元素獲取的相關(guān)技巧,需要的朋友可以參考下2015-07-07webpack進(jìn)階——緩存與獨(dú)立打包的用法
本篇文章主要介紹了webpack進(jìn)階——緩存與獨(dú)立打包的用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08JS腳本加載后執(zhí)行相應(yīng)回調(diào)函數(shù)的操作方法
本文主要講解怎么在成功加載 js 文件后再執(zhí)行相應(yīng)回調(diào)任務(wù),對(duì)JS腳本加載后執(zhí)行相應(yīng)回調(diào)函數(shù)的操作方法感興趣的朋友,通過(guò)本文學(xué)習(xí)下吧2018-02-02JS圖片根據(jù)鼠標(biāo)滾動(dòng)延時(shí)加載的實(shí)例代碼
這篇文章介紹了,JS圖片根據(jù)鼠標(biāo)滾動(dòng)延時(shí)加載的實(shí)例代碼,有需要的朋友可以參考一下2013-07-07JS中如何將JSON數(shù)組轉(zhuǎn)化為url參數(shù)
這篇文章主要介紹了JS中如何將JSON數(shù)組轉(zhuǎn)化為url參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04使用ngrok+express解決本地環(huán)境中微信接口調(diào)試問(wèn)題
這篇文章主要介紹了使用ngrok+express解決本地環(huán)境中微信接口調(diào)試問(wèn)題,需要的朋友可以參考下2018-02-02