如何调整 iframe 的大小?
我们有这个小组项目,我被分配去调整某个 iframe
的大小。自上周以来,我阅读了大量论坛帖子,发现 iframe
本身无法调整大小。
有没有办法让我的 iframe
调整大小?
We have this group project and I was assigned to make a certain iframe
resizable. I've been reading lots of forum posts since last week, and I found out that iframe
itself can't be resizable.
Is there a way to make my iframe
resizable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这可以用纯 CSS 来完成,如下所示:
将高度和宽度设置为您想要的最小尺寸,它看起来只会变大,不会缩小。
实例:https://jsfiddle.net/xpeLja5t/
这种技术有对除 IE 之外的所有主流浏览器都有良好的支持。
This can be done in pure CSS, like so:
Set the height and width to the minimum size you want, it only seems to grow, not shrink.
Live example: https://jsfiddle.net/xpeLja5t/
This technique has good support in all major browsers except IE.
我花了一个多小时找到一些东西,这让我找到了一个可行的解决方案:
在 Chrome 中,这个解决方案仍然存在一些问题。使用运行代码片段,然后整页(因此,如果
IFRAME
嵌入到其他IFRAMEs)。此外,调整大小手柄(位于右下角)显示得有点微妙,调整大小通常会随着鼠标停止。
在 Firefox 中,您现在可以使用 pure 调整
iframe
的大小CSS 也是如此。It took more than 1 hour to find something which led me to a working solution:
In Chrome this solution still has some issues at my side. Use Run code snippet and then Full Page (so it does not seem to work if the
IFRAME
is embedded in otherIFRAME
s). Also the resize handle (in the right bottom corner) is shown a bit subtle, and resizing often stops to follow the mouse.In Firefox you can resize the
iframe
now using pure CSS, too.这些步骤将使 iframe 可调整大小:
创建一个用于调整大小的 div。例如:
将 style 标签应用于 div 的样式。
包括 jQuery 和jQuery 用户界面
执行
可调整大小
These steps will make an iframe resizable:
Create a div for resizing. eg:
Apply the style tag for style of div.
Include jQuery & jQuery UI
Execute
Resizable
使用 jQuery UI...
HTML:
JavaScript:
jsFiddle: http://jsfiddle.net/B5vHQ/97/
说明:
由于 jQuery UI 的
ressized
插件无法直接在 iframe 上工作,因此将 iframe 包装在 div 中并调整 div 的大小 反而。上面代码中的resize
事件确保 iframe 调整大小以匹配 div。然而,此方法还有一个额外的问题 - iframe 不会将鼠标移动事件传递给其父元素。为了解决这个问题,
start
和stop
事件在调整大小期间用另一个元素覆盖 iframe,以便ressized
插件可以跟踪鼠标动作正确。出于某种原因,必须为包装器 div 指定明确的大小才能使调整大小起作用。在大多数情况下,这意味着 iframe 需要设置相同的大小,以便它从一开始就与 div 的大小相匹配。
With jQuery UI...
HTML:
JavaScript:
jsFiddle: http://jsfiddle.net/B5vHQ/97/
Explanation:
Since jQuery UI's
resizable
plugin won't work on an iframe directly, wrap the iframe in a div and resize the div instead. Theresize
event in the code above ensures that the iframe resizes to match the div.There's an additional problem with this method, however - iframes don't pass mouse movement events to their parent element. To work around that, the
start
andstop
events cover the iframe up with another element during the resize, so that theresizable
plugin can track mouse movement correctly.For some reason, giving the wrapper div an explicit size is necessary for the resizing to work. In most cases, that means that the iframe needs the same size set, so thatit matches the size of the div right from the start.
如果您还没有找到解决方案,我已经成功:
Jquery:
HTML:
我希望它有帮助
In case you haven't found the solution yet, I've had success with:
Jquery:
HTML:
I hope it helps
我找到了您的解决方案:
HTML:
JQUERY:
I found your solution:
HTML:
JQUERY:
我发现的最佳解决方案是将 iframe 的宽度和高度设置为 100% 并将 iframe 放在 div 标签内。这是您使用 CKEditor 等工具看到的基本解决方案。
例如,我有一些 jQuery 对话框在 Utah's Disclosures Site。如果单击 or 的报告或语句,您将看到弹出一个对话框,其中包含 iframe。希望有帮助。
The best solution I've found is to set the width and height of the iframe to 100% and put the iframe within a div tag. This is the basic solution you see with tools like CKEditor.
For an example, I have some jQuery dialogs that do just that on the Utah's Disclosures Site. If you click on a report or statement of or, you'll see a dialog that pops up with an iframe in it. Hope it helps.
非常简单,
首先 -
添加 jquery 和 jquery-ui -
然后 -
最后 -
完成!
表单 http://jqueryui.com/ressized/
very easy,
first -
add jquery and jquery-ui -
then -
last -
Its done!
form http://jqueryui.com/resizable/