iframe 的高度根據內容自動調整

來源:互聯網
上載者:User

我們在做項目的時候如果用到了Ifram的話,可能會有一個很苦惱的問題,那就是 Ifram 的高度可以被頁面撐大,

但是撐大後就回不來了,那麼,怎麼讓iframe 的高度自動根據iframe 裡面頁面的內容的高度來調整呢,很簡單,

只需將如下 js 代碼 copy 到 Ifram 的 src 屬性所指向的頁面中就可以了。

<script>function f_frameStyleResize(targObj){var targWin = targObj.parent.document.all[targObj.name];if(targWin != null) {var HeightValue = targObj.document.body.scrollHeightif(HeightValue < 475){HeightValue = 475} //不小於475targWin.style.pixelHeight = HeightValue;}}function f_iframeResize(){bLoadComplete = true; f_frameStyleResize(self);}var bLoadComplete = false;window.onload = f_iframeResize;</script>

----以上代碼寫在被 iframe 包含的頁面內,即 iframe 的 src 所指向的頁面
注意:iframe必須要有name屬性,否則無效。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.