iframe 动态调整高度

发布于 2024-12-02 16:30:09 字数 529 浏览 0 评论 0原文

您好,我目前有 2 个页面(index.html 和 iframe_contents.html)。两者都在同一域中。

我目前正在尝试让 iframe 根据内容大小动态调整大小。

我用它来帮助我 http://benalman.com/code/projects /jquery-resize/examples/resize/ 如果 iframe_contents body 标签在 Firefox 和 IE 7/8/9 上变大或变小,它就会起作用,但对于 webkit,它只能增长而永远不能缩小

我已经将其缩小到 iframe_contents.html 中的 body 标记,当内容高度更改时不会缩小,但仅在 iframe 中缩小。当 iframe_contents.html 不在 iframe 中时,如果我缩小/放大元素,主体的整体高度会发生变化。

这是一个 webkit 特定问题吗?

Hi I currently have 2 pages (index.html and iframe_contents.html). Both are on the same domain.

I am currently trying to get the iframe to dynamically resize based on the contents size.

I was using this to assist me http://benalman.com/code/projects/jquery-resize/examples/resize/ and it works if the iframe_contents body tag gets larger or smaller on Firefox and IE 7/8/9 but for webkit it only can grow and can never shrink

I've narrowed it down to the body tag in iframe_contents.html not shrinking when content height changes but only in the iframe. When iframe_contents.html is not in a iframe if I shrink / enlarge elements the bodies overall height changes.

Is this a webkit specific issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疧_╮線 2024-12-09 16:30:09

在这里阅读了很多答案后,他们都遇到了同样的问题,即在需要时不调整大小。我认为大多数人只是在框架加载后一次性调整大小,所以可能不在乎。每当窗口大小发生变化时,我都需要再次调整大小。所以对我来说,如果他们让窗口变窄,iframe 会变得非常高,然后当他们让窗口变大时,它应该会再次变短。在某些浏览器上不会发生这种情况,因为我可以使用 DOM 检查器(FireBug/Chrome 开发工具)找到的scrollHeight、clientHeight、jquery height() 和任何其他高度在 iframe 启动后不会报告正文或 html 高度变短。变得更宽。就像身体的最小高度设置为 100% 之类的。

对我来说,解决方案是使 iframe 高度为 0,然后检查滚动高度,然后设置为该值。为了避免页面上的滚动条跳跃,我将父级(包含 iframe)的高度设置为 iframe 高度,以在执行此操作时保持总页面大小固定。

我希望我有一个更干净的示例,但这是我的代码:

        $(element).parent().height($(element).height());
        $(element).height(0);
        $(element).height($(element).contents().height());
        $(element).parent().height("");

element 是我的 iframe。

iframe 具有 width: 100% 样式设置,并且位于具有默认样式(块)的 div 内。

代码是jquery,将div高度设置为iframe高度,然后将iframe设置为0高度,然后将iframe设置为内容高度。如果我删除将 iframe 设置为 0 高度的行,则 iframe 将在需要时变大,但永远不会变小。

After reading lots of answers here they all had the same issue with not resizing smaller when needed. I think most people are just doing a one-off resizing after the frame loads, so maybe don't care. I need to resize again anytime the window size changes. So for me, if they made the window narrow the iframe would get very tall, then when they make the window larger it should get shorter again. This wasn't happening on some browsers because the scrollHeight, clientHeight, jquery height() and any other height I could find with DOM inspectors (FireBug/Chrome Dev Tools) did not report the body or html height as being shorter after the iframe was made wider. Like the body had min-height 100% set or something.

For me the solution was to make the iframe 0 height, then check the scrollHeight, then set to that value. To avoid the scrollbar on my page jumping around, I set the height of the parent (that contains the iframe) to the iframe height to keep the total page size fixed while doing this.

I wish I had a cleaner sample, but here is the code I have:

        $(element).parent().height($(element).height());
        $(element).height(0);
        $(element).height($(element).contents().height());
        $(element).parent().height("");

element is my iframe.

The iframe has width: 100% style set and is inside a div with default styles (block).

Code is jquery, and sets the div height to the iframe height, then sets iframe to 0 height, then sets iframe to the contents height. If I remove the line that sets the iframe to 0 height, the iframe will get larger when needed, but never smaller.

〃温暖了心ぐ 2024-12-09 16:30:09

这可能对您没有多大帮助,但这是我们在 iframe_contents.html 页面中拥有的一个函数。它将尝试以一种自调整大小、跨浏览器、纯 JavaScript 的方式调整加载它的 iframe 的大小:

function makeMeFit() {
    if (top.location == document.location) return; // if we're not in an iframe then don't do anything
    if (!window.opera && !document.mimeType && document.all && document.getElementById) {
        parent.document.getElementById('youriframeid').style.height = (this.document.body.offsetHeight + 30) + "px";
    } else if (document.getElementById) {
        parent.document.getElementById('youriframeid').style.height = (this.document.body.scrollHeight + 30) + "px"
    }
}

您可以在 resize() 事件中调用它或者发生改变页面高度的事件后。该方法中的功能测试应分离出 WebKit 浏览器并选择正确的高度属性。

This may not help you much but here is a function we have in what would be your iframe_contents.html page. It will attempt to resize the iframe in which it is loaded in a sort of self-resizing, cross-browserish, pure-JavaScript kind of way:

function makeMeFit() {
    if (top.location == document.location) return; // if we're not in an iframe then don't do anything
    if (!window.opera && !document.mimeType && document.all && document.getElementById) {
        parent.document.getElementById('youriframeid').style.height = (this.document.body.offsetHeight + 30) + "px";
    } else if (document.getElementById) {
        parent.document.getElementById('youriframeid').style.height = (this.document.body.scrollHeight + 30) + "px"
    }
}

You could put calls to it in a resize() event or following an event that changes the height of your page. The feature-testing in that method should separate out WebKit browsers and pick the correct height property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文