西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)其它知識 → iframe 高度設(shè)置、iframe 自適應(yīng)高度方法總結(jié)

iframe 高度設(shè)置、iframe 自適應(yīng)高度方法總結(jié)

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2014/3/13 22:14:40字體大。A-A+

作者:西西點擊:9316次評論:0次標(biāo)簽: iframe

華碩MultiFrame軟件v1.0.22 官方安裝版
  • 類型:系統(tǒng)其它大。3.0M語言:中文 評分:5.0
  • 標(biāo)簽:
立即下載

謂iframe自適應(yīng)高度,就是,基于界面美觀和交互的考慮,隱藏了iframe的border和scrollbar,讓人看不出它是個iframe。如果iframe始終調(diào)用同一個固定高度的頁面,我們直接寫死iframe高度就可以了。而如果iframe要切換頁面,或者被包含頁面要做DOM動態(tài)操作,這時候,就需要程序去同步iframe高度和被包含頁的實際高度了。

如果iframe的高度沒有確定,那將是初始的高度。
iframe是網(wǎng)頁中的一部分,其大小還要受到網(wǎng)頁的限制,設(shè)置最高可以使用height="100%"。
基本上解決iframe超出的高度都是增加了滾動條來實現(xiàn)的,很簡單,如果你iframe中的信息超出了一屏幕,你就必須使用滾動條了。

開始用的時候還不行,后來發(fā)現(xiàn)是因為js跨域問題,沒有權(quán)限。后來設(shè)置了window.document.domain 就可以了,用的是jquery代碼2方法。

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

跨域的時候,由于js的同源策略,父頁面內(nèi)的js不能獲取到iframe頁面的高度。需要一個頁面來做代理。
方法如下:假設(shè)www.a.com下的一個頁面a.html要包含www.b.com下的一個頁面c.html。
我們使用www.a.com下的另一個頁面agent.html來做代理,通過它獲取iframe頁面的高度,并設(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; 
    })();
</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的高度和寬度(因為agent.html在www.a.com下,所以操作a.html時不受JavaScript的同源限制)

超級簡單的方法,也不用寫什么判斷瀏覽器高度、寬度啥的。
下面的兩種方法自選其一就行了。一個是放在和iframe同頁面的,一個是放在test.html頁面的。
注意別放錯地方了哦。

下面是其他兩種方法:
iframe代碼,注意要寫ID

<iframe src="test.html" id="main" width="700" height="300" frameborder="0" scrolling="auto"></iframe>

jquery代碼1:

//注意:下面的代碼是放在test.html調(diào)用
$(window.parent.document).find("#main").load(function(){
var main = $(window.parent.document).find("#main");
var thisheight = $(document).height()+30;
main.height(thisheight);
});

jquery代碼2:

//注意:下面的代碼是放在和iframe同一個頁面調(diào)用
$("#main").load(function(){
var mainheight = $(this).contents().find("body").height()+30;
$(this).height(mainheight);
}); 

第二種有效,不過要注意一點是,增加的JS要寫在iframe下面,放在頭部是測試沒有效果。
測試代碼:
<iframe id="mainframe" name="mainframe" marginwidth="0" marginheight="0" src="/Home/Activitylist" frameborder="0" width="100%" scrolling="no" height="100%"></iframe>
<script type="text/javascript">
//注意:下面的代碼是放在和iframe同一個頁面調(diào)用,放在iframe下面
$("#mainframe").load(function () {
var mainheight = $(this).contents().find("body").height() + 30;
 $(this).height(mainheight);
});
</script>

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)