解決iframe高度自適應問題有兩種方法
1. pym
2.手動設置iframe的高度
本文主要是總結第二種實現方式,由於第一種pym.js插件我沒用懂css
若是使用iframe時,遇到如下的需求:html
*須要起個服務,不要直接在本地打開html5
這是個只讀屬性,返回指定的iframe的window對象
拿到這個對象,就能夠根據正常網頁的方法拿到嵌入(子)網頁的文檔高度,而後把值附給父頁面的iframe的height。jquery
function setIframeHeight(iframe) { if (iframe) { var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow; if (iframeWin.document.body) { iframe.height = iframeWin.document.body.scrollHeight; } } };
爲了監測網頁高度是否由於點擊了某個下拉按鈕高度變化,咱們創建一個定時器任務,這樣一直監視着子網頁的高度。git
setInterval(function () { setIframeHeight($('#iframe')[0]) }, 200)
頁面有可能同時存在多個iframegithub
$(".flexiframe").each(function (index) {//iframe公用類名:flexiframe var that = $(this); (function () { setInterval(function () { setIframeHeight(that[0]) }, 200) })(that) });
若是此時,你發現當子頁面高度變高時,高度能夠自適應的變高,但在變低時,會發現父頁面的高度並無向咱們想象的隨着子頁面下降,致使底部留有大面積空白的問題,請繼續往下看跨域
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
父頁面:a.htmlapp
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>a</title> </head> <body style="background-color:red"> <iframe id="iFrame" class="flexiframe" src="./b.html" style="padding: 0;width:100%;" marginwidth="0" frameborder="no" scrolling="no" height="2000px"></iframe> <!-- <iframe id="iFrame1" class="flexiframe" src="./b.html" style="padding: 0;width:100%" marginwidth="0" frameborder="no" scrolling="no"></iframe> --> <!-- <iframe id="iFrame2" class="flexiframe" src="./b.html" style="padding: 0;width:100%" marginwidth="0" frameborder="no" scrolling="no"></iframe> --> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script src="./flexiframe.js"></script> </body> </html>
./flexiframe.jsflex
// 使用前先將子頁面文檔聲明改成 //<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> function setIframeHeight(iframe) { if (iframe) { var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow; if (iframeWin.document.body) { iframe.height = iframeWin.document.body.scrollHeight; } } }; $(".flexiframe").each(function (index) { var that = $(this); (function () { setInterval(function () { setIframeHeight(that[0]) }, 200) })(that) });
子頁面b.html,分別註釋兩種聲明方式,點擊變大變小按鈕看一下右側滾動條的變化,(細節能夠F12看一下iframe種的html和body跟div的高度關係)ui
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> --> <!DOCTYPE html> <div id="b_conntent"> <div style="width:100px;background-color:blue"> <div style="height:2000px"> <button id="btn">小</button> </div> </div> </div> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> $('#btn').on('click', function () { if ($(this).text() == '小') { $(this).text('大').parent().css('height', '200px') } else { $(this).text('小').parent().css('height', '2000px') } }) </script>
1.有時間仍是要了解下html的進化歷史。2.本文flexiframe.js能夠直接使用,可是隻支持同源下父頁面能夠經過contentWindow獲取子頁面的內容高度,跨域的下次再說。3.歡迎指出問題或留言加深本文的深度,例如html5已經不須要聲明DTD,可是我解決這個問題不得不改一下DOCTYPE,退化到html4,爲何在html5中子頁面html和body的高度不是由內部的內容決定的,而是等於父元素iframe的高度?