使 IFrame 动态调整大小
我有以下 HTML 页面,其中有三个 iframe。我想允许用户使用 Javascript 手动调整 iframe 的高度和宽度。
<body>
<table>
<tr>
<td colspan="2" id = "freebase_td">
<iframe id = "freebase_frame" src="" width="100%" height="400px"></iframe>
</td>
</tr>
<tr>
<td align="left" id = "wiki_td">
<iframe id = "wiki_frame" src="" width="100%" height="400px"></iframe>
</td>
<td align="right" id = "imdb_td">
<iframe id = "imdb_frame" src="" width="100%" height="400px"></iframe>
</td>
</tr>
</table>
</body>
I have the following HTML page in which I have three iframes. I would like to allow users to resize the height and width of the iframes manually using Javascript.
<body>
<table>
<tr>
<td colspan="2" id = "freebase_td">
<iframe id = "freebase_frame" src="" width="100%" height="400px"></iframe>
</td>
</tr>
<tr>
<td align="left" id = "wiki_td">
<iframe id = "wiki_frame" src="" width="100%" height="400px"></iframe>
</td>
<td align="right" id = "imdb_td">
<iframe id = "imdb_frame" src="" width="100%" height="400px"></iframe>
</td>
</tr>
</table>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你最好的选择是使用一个库来做到这一点,例如 jQueryUI Resizing - 有正确执行此操作会出现很多浏览器间的怪癖。特别是当您需要允许用户指定任意大小/调整大小时,可拖动调整大小手柄几乎总是允许用户调整元素大小的最直观方法。如果您只是调整 iframe 的大小,那么直接设置 iframe 的宽度/高度应该没问题,但这并不能以直观的方式从用户那里获取新的宽度/高度。
另请参阅 使用 jQuery UI 调整 iFrame 大小 - 您需要将 iframe 包装在 div 中,将 iframe 设置为 height=100% width=100%,并调整 div 的大小。如果您将裸 iframe 设置为可拖动,则拖动调整大小将无法正常工作。 (这是某些浏览器中的事件冒泡限制,而不是 jQuery 本身的错误。)
Your best bet really is to use a library to do this, such as jQueryUI Resizable -- there are a lot of inter-browser quirks with doing this right. Especially when you need to allow the user to specify an arbitrary size/resize, and a draggable resize handle is almost always the most intuitive way to allow users to resize an element. Setting the width/height of the iframe directly should be okay if you are just resizing the iframe, but that doesn't do anything to get the new width/height from the user in an intuitive way.
Also see Resizing iFrame with jQuery UI -- you need to wrap the iframe in a div, set the iframe to height=100% width=100%, and make the div resizable. Drag-to-resize will not work correctly if you make the bare iframe Draggable. (This is an event bubbling limitation in some browsers, not a jQuery bug per se.)
在现代浏览器中你不需要 javascript。只是一点 html+css 的技巧。将您的 iframe 封装在启用了 resize 属性的 div 中。
演示: https://jsfiddle.net/k53a41ej/
you don't need javascript for that in modern browsers. just a little html+css trick. encapsulate your iframe in a div with resize property enabled.
demo: https://jsfiddle.net/k53a41ej/
我选择像诅咒一样避开 iFrame。我知道这不是您所要求的,但请查看 jQueryUI 的可调整大小。
如果您更改基础结构,您可能会发现这是一个更具可扩展性/长期的解决方案。
注意:可调整大小可以应用于任何 DOM 元素!
I choose to avoid iFrames like a curse. I know this isnt exaclty what you asked for but check out jQueryUI's Resizable.
If you change your foundational structure you might find this a much more scalable/long term solution.
Note: Resizable can be applied to ANY DOM element!
这不行吗?
Does this not work?