调整 iframe 的宽度和高度以适应其中的内容
我需要一个解决方案来自动调整iframe
的宽度
和高度
以勉强适合其内容。 重点是,在加载 iframe
后可以更改宽度和高度。 我想我需要一个事件操作来处理 iframe 中包含的正文尺寸的变化。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
删除iframe
如果内容只是一个非常简单的html,最简单的方法是用javascript HTML代码
: Javascript代码:
If the content is just a very simple html, the simplest way is to remove the iframe with javascript
HTML code:
Javascript code:
如果你正在寻找一个无 jQuery 跨源解决方案,你可能想看看我的想法:
If you are looking for a no jQuery cross-origin solution, you might want to look at my idea:
如果您不想使用 jQuery,这里有一个跨浏览器解决方案:
Here is a cross-browser solution if you don't want to use jQuery:
在我尝试了地球上的一切之后,这对我来说真的很有效。
索引.html
After I have tried everything on the earth, this really works for me.
index.html
上下文
我必须自己在网络扩展的上下文中执行此操作。 此 Web 扩展将一些 UI 注入到每个页面中,并且此 UI 位于
iframe
内。 iframe 内的内容是动态的,因此我必须重新调整 iframe 本身的宽度和高度。我使用 React 但这个概念适用于每个库。
我的解决方案(假设您同时控制页面和 iframe)
在 iframe 内,我更改了 body 样式以具有非常大的尺寸。 这将允许内部元素使用所有必要的空间进行布局。 使
width
和height
100% 对我来说不起作用(我猜是因为 iframe 有默认的width = 300px
和height = 150px
)然后我将所有 iframe UI 注入到 div 中并给它一些样式
在这个
#ui-root
中渲染我的应用程序后(在 React 中我在componentDidMount
中执行此操作)我计算此 div 的尺寸并使用window.postMessage
将它们同步到父页面:在父框架中,我执行如下操作:
Context
I had to do this myself in a context of a web-extension. This web-extension injects some piece of UI into each page, and this UI lives inside an
iframe
. The content inside theiframe
is dynamic, so I had to readjust the width and height of theiframe
itself.I use React but the concept applies to every library.
My solution (this assumes that you control both the page and the iframe)
Inside the
iframe
I changedbody
styles to have really big dimensions. This will allow the elements inside to lay out using all the necessary space. Makingwidth
andheight
100% didn't work for me (I guess because the iframe has a defaultwidth = 300px
andheight = 150px
)Then I injected all the iframe UI inside a div and gave it some styles
After rendering my app inside this
#ui-root
(in React I do this insidecomponentDidMount
) I compute the dimensions of this div and sync them to the parent page usingwindow.postMessage
:In the parent frame I do something like this:
我使用此代码在所有 iframe(带有 autoHeight 类)加载到页面上时自动调整它们的高度。 经过测试,它可以在 IE、FF、Chrome、Safari 和 Opera 中运行。
I am using this code to autoadjust height of all iframes (with class autoHeight) when they loads on page. Tested and it works in IE, FF, Chrome, Safari and Opera.
这是一个可靠的证明解决方案
html
This is a solid proof solution
the html
如果您可以控制 IFRAME 内容和父窗口,那么您需要 iFrame Resizer。
该库可以自动调整相同和跨域 iFrame 的高度和宽度,以适应其包含的内容。 它提供了一系列功能来解决使用 iFrame 时最常见的问题,其中包括:
If you can control both IFRAME content and parent window then you need the iFrame Resizer.
This library enables the automatic resizing of the height and width of both same and cross domain iFrames to fit their contained content. It provides a range of features to address the most common issues with using iFrames, these include:
如果您可以接受固定宽高比,并且想要一个响应式 iframe,那么此代码将对您有用。 这只是 CSS 规则。
iframe 必须有一个 div 作为容器。
源代码基于此网站和Ben Marshall 有一个很好的解释。
If you can live with a fixed aspect ratio and you would like a responsive iframe, this code will be useful to you. It's just CSS rules.
The iframe must have a div as container.
The source code is based on this site and Ben Marshall has a good explanation.
我在这里阅读了很多答案,但几乎每个人都给出了某种跨源框架块。
错误示例:
相关线程中的答案也是如此:
Make iframe 自动根据内容调整高度而不使用滚动条?
我不想要使用第三方库,例如 iFrame Resizer 或类似的库。
@bboydflo 的答案很接近,但我缺少一个完整的示例。 https://stackoverflow.com/a/52204841/3850405
我正在使用
width="100%"
适用于iframe
,但也可以修改代码以使用宽度。这就是我解决为
iframe
设置自定义高度的方法:嵌入式
iframe
:iframe 源
,来自Create React App
的示例code> 但仅使用HTML
和JS
。setInterval
的代码当然可以修改,但它对于动态内容非常有效。setInterval
仅在内容嵌入到iframe
中时激活,并且postMessage
仅在高度发生变化时发送消息。您可以在此处阅读有关 Window.postMessage() 的更多信息,但该描述非常适合我们想要实现的目标:
https://developer.mozilla.org/en-US/ docs/Web/API/Window/postMessage
如果你想要
iframe
的宽度和高度为 100%,我会这样做:来源:
https://stackoverflow.com/a/27853830/3850405
I have been reading a lot of the answers here but nearly everyone gave some sort of cross-origin frame block.
Example error:
The same for the answers in a related thread:
Make iframe automatically adjust height according to the contents without using scrollbar?
I do not want to use a third party library like
iFrame Resizer
or similar library either.The answer from @bboydflo is close but I'm missing a complete example. https://stackoverflow.com/a/52204841/3850405
I'm using
width="100%"
for theiframe
but the code can be modified to work with width as well.This is how I solved setting a custom height for the
iframe
:Embedded
iframe
:iframe source
, example fromCreate React App
but onlyHTML
andJS
is used.The code with
setInterval
can of course be modified but it works really well with dynamic content.setInterval
only activates if the content is embedded in aiframe
andpostMessage
only sends a message when height has changed.You can read more about
Window.postMessage()
here but the description fits very good in what we want to achieve:https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
If you want 100% width and height for
iframe
I would do it like this:Source:
https://stackoverflow.com/a/27853830/3850405
我稍微修改了 Garnaph 上面的伟大解决方案。 看起来他的解决方案根据事件发生前的大小修改了 iframe 大小。 对于我的情况(通过 iframe 提交电子邮件),我需要在提交后立即更改 iframe 高度。 例如,提交后显示验证错误或“谢谢”消息。
我刚刚消除了嵌套的 click() 函数并将其放入我的 iframe html 中:
对我有用,但不确定跨浏览器功能。
I slightly modified Garnaph's great solution above. It seemed like his solution modified the iframe size based upon the size right before the event. For my situation (email submission via an iframe) I needed the iframe height to change right after submission. For example show validation errors or "thank you" message after submission.
I just eliminated the nested click() function and put it into my iframe html:
Worked for me, but not sure about cross browser functionality.
使用以上方法都无法工作。
javascript:
html:
环境: IE 10、Windows 7 x64
all can not work using above methods.
javascript:
html:
Env: IE 10, Windows 7 x64
经过一番试验,我找到了另一种解决方案。 我最初尝试了标记为这个问题的“最佳答案”的代码,但它不起作用。 我的猜测是因为我当时程序中的iframe是动态生成的。 这是我使用的代码(它对我有用):
正在加载的 iframe 内的 Javascript:
这是 iframe 的 html:
这是我的 iframe 中的所有代码:
我已经在 chrome 中进行了测试,并在 firefox 中进行了一些测试(在 windows xp 中)。 我还有更多测试要做,所以请告诉我这对你来说如何。
I figured out another solution after some experimenting. I originally tried the code marked as 'best answer' to this question and it didn't work. My guess is because my iframe in my program at the time was dynamically generated. Here is the code I used (it worked for me):
Javascript inside the iframe that is being loaded:
Here is the html for the iframe:
Here is all the code within my iframe:
I have done testing in chrome and a little in firefox (in windows xp). I still have more testing to do, so please tell me how this works for you.
可以制作一个“类似幽灵”的 IFrame,其行为就像它不存在一样。
请参阅 http://codecopy.wordpress.com/2013 /02/22/ghost-iframe-crossdomain-iframe-resize/
基本上,您使用中描述的事件系统
parent.postMessage(..)
https://developer.mozilla.org/en-US/docs/DOM /window.postMessage
这适用于所有现代浏览器!
It is possible to make a "ghost-like" IFrame that acts like it was not there.
See http://codecopy.wordpress.com/2013/02/22/ghost-iframe-crossdomain-iframe-resize/
Basically you use the event system
parent.postMessage(..)
described inhttps://developer.mozilla.org/en-US/docs/DOM/window.postMessage
This works an all modern browsers!
这里有几种方法:
和另一种
隐藏滚动的替代方法,如上所示,用第二个
代码
进行黑客攻击,有两种替代方法要隐藏 iFrame 的滚动条,将父级设置为“overflow:hidden”以隐藏滚动条,然后使 iFrame 消失高达 150% 的宽度和高度,这会强制滚动条位于页面之外,并且由于主体没有滚动条,人们可能不会期望 iframe 超出页面的边界。 这会隐藏全宽 iFrame 的滚动条!
来源:设置 iframe 自动高度
Here are several methods:
AND ANOTHER ALTERNATIVE
TO HIDE SCROLLING WITH 2 ALTERNATIVES AS SHOWN ABOVE
HACK WITH SECOND CODE
To hide the scroll-bars of the iFrame, the parent is made "overflow:hidden" to hide scrollbars and the iFrame is made to go upto 150% width and height which forces the scroll-bars outside the page and since the body doesn't have scroll-bars one may not expect the iframe to be exceeding the bounds of the page. This hides the scrollbars of the iFrame with full width!
source: set iframe auto height
如果有人到达这里:
当我从 iframe 中删除 div 时,我遇到了解决方案的问题 - iframe 并没有变短。
有一个 Jquery 插件可以完成这项工作:
http://www.jqueryscript.net/layout/jQuery-Plugin-For-Auto-Resizing-iFrame-iFrame-Resizer.html
In case someone getting to here:
I had a problem with the solutions when I removed divs from the iframe - the iframe didnt got shorter.
There is an Jquery plugin that does the job:
http://www.jqueryscript.net/layout/jQuery-Plugin-For-Auto-Resizing-iFrame-iFrame-Resizer.html
我发现这个缩放器工作得更好:
注意样式对象已被删除。
I found this resizer to work better:
Note the style object is removed.
在 jQuery 中,这对我来说是最好的选择,这对我真的很有帮助! 我等着帮助你!
iframe
jQuery
In jQuery, this is the best option to me, that really help me!! I wait that help you!
iframe
jQuery
显然有很多场景,但是,我的文档和 iframe 具有相同的域,并且我能够将其附加到我的 iframe 内容的末尾:
这“找到”父容器,然后设置添加模糊因子的长度50 像素以删除滚动条。
没有任何东西可以“观察”文档高度的变化,这对于我的用例来说是不需要的。 在我的回答中,我确实引入了一种引用父容器的方法,而不使用嵌入到父/iframe 内容中的 id。
Clearly there are lots of scenarios, however, I had same domain for document and iframe and I was able to tack this on to the end of my iframe content:
This 'finds' the parent container and then sets the length adding on a fudge factor of 50 pixels to remove the scroll bar.
There is nothing there to 'observe' the document height changing, this I did not need for my use case. In my answer I do bring a means of referencing the parent container without using ids baked into the parent/iframe content.
iframe 样式:
iframe 标记:
脚本标记:
ifram style:
iframe tag:
Script tag:
这就是我的做法(在 FF/Chrome 中测试):
This is how I would do it (tested in FF/Chrome):
我知道这篇文章很旧,但我相信这是另一种方法。 我刚刚在我的代码上实现了。 在页面加载和页面调整大小时都能完美运行:
I know the post is old, but I believe this is yet another way to do it. I just implemented on my code. Works perfectly both on page load and on page resize:
Javascript 放置在标题中:
这里是 iframe html 代码:
Css 样式表
>
Javascript to be placed in header:
Here goes iframe html code:
Css stylesheet
>
对于 angularjs 指令属性:
注意百分比,我插入它是为了可以对抗通常对 iframe、文本、广告等进行的缩放,如果没有实现缩放,只需输入 0
For angularjs directive attribute:
Notice the percentage, i inserted it so that you can counter scaling usually done for iframe, text, ads etc, simply put 0 if no scaling is implementation
这就是我在加载或事情发生变化时的做法。
This is how I did it onload or when things change.
用于嵌入的单线解决方案:
从最小尺寸开始,逐渐增加到内容尺寸。 不需要脚本标签。
one-liner solution for embeds:
starts with a min-size and increases to content size. no need for script tags.
跨浏览器jQuery 插件。
跨浏览器、跨域库,名为 iframe-resizer,使用
resizeObserver
和mutationObserver
来保持 iFrame 的大小适合内容,以及postMessage
来在 iFrame 和主机页面之间进行通信。 有 React、Vue 和 jQuery 版本。Cross-browser jQuery plug-in.
Cross-bowser, cross domain library called iframe-resizer that uses
resizeObserver
andmutationObserver
to keep iFrame sized to the content andpostMessage
to communicate between iFrame and host page. Has versions for React, Vue and jQuery.迄今为止给出的所有解决方案仅考虑一次调整大小。 您提到您希望能够在修改内容后调整 iFrame 的大小。 为此,您需要在 iFrame 内执行一个函数(一旦内容发生更改,您需要触发一个事件来表明内容已更改)。
我被这个问题困扰了一段时间,因为 iFrame 内部的代码似乎仅限于 iFrame 内部的 DOM(并且无法编辑 iFrame),而在 iFrame 外部执行的代码则被困在 iFrame 外部的 DOM 中(并且无法编辑) t 获取来自 iFrame 内部的事件)。
解决方案来自于发现(通过同事的帮助)jQuery 可以被告知要使用什么 DOM。 在本例中,是父窗口的 DOM。
因此,这样的代码可以满足您的需要(在 iFrame 内运行时):
All solutions given thus far only account for a once off resize. You mention you want to be able to resize the iFrame after the contents are modified. In order to do this, you need to execute a function inside the iFrame (once the contents are changed, you need to fire an event to say that the contents have changed).
I was stuck with this for a while, as code inside the iFrame seemed limited to the DOM inside the iFrame (and couldn't edit the iFrame), and code executed outside the iFrame was stuck with the DOM outside the iFrame (and couldn't pick up an event coming from inside the iFrame).
The solution came from discovering (via assistance from a colleague) that jQuery can be told what DOM to use. In this case, the DOM of the parent window.
As such, code such as this does what you need (when run inside the iFrame) :
如果 iframe 内容来自同一域,这应该会很好用。 但它确实需要 jQuery。
要动态调整大小,您可以这样做:
然后在 iframe 加载的页面上添加以下内容:
If the iframe content is from the same domain this should work great. It does require jQuery though.
To have it resize dynamically you could do this:
Then on the page that the iframe loads add this: