2016-06-10 111 views
2

我一直在研究這個了幾天,我卡住了...我看到這個頁面的內容:調整大小的iframe高度時,iframe的變化(同一域)

Resizing an iframe based on content

與此頁:

Auto resize iframe height when the height of the iframe contents change (same domain)

看來這是比較容易調整的iframe當包含I幀首次加載,但緬因州的index.html一次的iframe更改的內容我無法弄清楚如何重新 - 大小e iframe。

我的index.html:

<body> 
<iframe seamless frameborder="0" name="top" id="top" src="venues.php" width="100%" height="100" /></iframe> 
<iframe seamless frameborder="0" name="main" id="main" src="admin.php" width="100%" height="2000" /></iframe> 

當venues.php負荷是100像素(這是罰​​款),但是當在(內部,同一個域)的venues.php內部鏈接有人clisk頁面我需要調整「頂部」框架高度以適應新的內容。

我試過一堆解決方案,我總是隻能在第一次加載時調整大小,而不是當有人在裏面點擊時調整大小。

感謝

+0

我已經解決了在框架中使用JS向容器中的JS發送消息。該消息說明框架需要多大,容器中的JS比調整控件大小。 – Richard

+0

你能告訴我一個例子,或者至少告訴我你是如何發送消息到容器?我在印象中(從我閱讀過的其中一個鏈接中看到的東西)你無法通過孩子與父母進行溝通 – marcnyc

+0

你有沒有看過: - http://stackoverflow.com/questions/12082125/resize-iframe -on-content-refresh-or-update –

回答

1

您可以使用mutation observer事件和postMessage API,這是非常困難的創建和執行腳本,可以協商頻繁改變的iframe的尺寸測量。我放棄了從頭開始編寫一些東西,用這個小插件:

https://github.com/davidjbradshaw/iframe-resizer

Plunker的IFRAME發生調整時檢測mutation observer事件火的時候演示如何使用此插件。

PLUNKER

+0

非常感謝Plunker,幫了我很多... – marcnyc

+0

沒問題,我很樂意提供幫助。 – zer00ne

0

你可以看看大衛Bradshaws混帳回購協議

他開發了一個漂亮的腳本做你在找什麼。

保持相同和跨域iFrames的大小,以支持它們的內容,支持>窗口/內容大小調整,頁面鏈接,嵌套和多個iFrames。 >(依賴免費,IE8 +)http://davidjbradshaw.github.io/iframe-resizer/

+0

我曾看過Bradshaw的iFrame Resizer,但無法讓它工作......事實上,在Chrome(本地)他的測試示例甚至沒有爲我工作 – marcnyc

+0

@marcnyc試試我的演示,Chrome 51測試很好。 – zer00ne