iframe自適應高度方法

來源:互聯網
上載者:User

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

如果iframe的高度沒有確定,那將是初始的高度。
iframe是網頁中的一部分,其大小還要受到網頁的限制,設定最高可以使用height="100%"。
基本上解決iframe超出的高度都是增加了捲軸來實現的,很簡單,如果你iframe中的資訊超出了一螢幕,你就必須使用捲軸了。

開始用的時候還不行,後來發現是因為js跨域問題,沒有許可權。後來設定了window.document.domain 就可以了,用的是jquery代碼2方法。

跨域下的iframe自適應高度

跨域的時候,由於js的同源策略,父頁面內的js不能擷取到iframe頁面的高度。需要一個頁面來做代理。
方法如下:假設www.a.com下的一個頁面a.html要包含www.b.com下的一個頁面c.html。
我們使用www.a.com下的另一個頁面agent.html來做代理,通過它擷取iframe頁面的高度,並設定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中獲得寬度值和高度值,並設定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調用
$(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同一個頁面調用
$("#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同一個頁面調用,放在iframe下面
$("#mainframe").load(function () {
var mainheight = $(this).contents().find("body").height() + 30;
 $(this).height(mainheight);
});
</script>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.