简体   繁体   English

iframe动态高度调整大小

[英]iframe dynamic height resizing

Hi I currently have 2 pages (index.html and iframe_contents.html). 嗨我目前有2页(index.html和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. 我目前正在尝试根据内容大小动态调整iframe。

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 我正在使用它来帮助我http://benalman.com/code/projects/jquery-resize/examples/resize/如果iframe_contents body标记在Firefox和IE 7/8/9上变得更大或更小,它可以工作webkit它只能成长,永远不会缩小

I've narrowed it down to the body tag in iframe_contents.html not shrinking when content height changes but only in the iframe. 我将iframe_contents.html中的body标签缩小到内容高度更改时不缩小但仅在iframe中缩小。 When iframe_contents.html is not in a iframe if I shrink / enlarge elements the bodies overall height changes. 当iframe_contents.html不在iframe中时,如果缩小/放大元素,则整体高度会发生变化。

Is this a webkit specific issue? 这是一个特定于webkit的问题吗?

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. 所以对我来说,如果他们让窗户缩小,那么iframe会变得非常高,那么当他们使窗户变大时,它会再次变短。 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. 在某些浏览器上没有发生这种情况,因为我在使用DOM检查器(FireBug / Chrome开发工具)时可以找到的scrollHeight,clientHeight,jquery height()和任何其他高度都没有报告iframe后身体或html高度更短做得更广。 Like the body had min-height 100% set or something. 像身体有最小高度100%设置或东西。

For me the solution was to make the iframe 0 height, then check the scrollHeight, then set to that value. 对我来说,解决方案是使iframe 0高度,然后检查scrollHeight,然后设置为该值。 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. 为了避免我页面上的滚动条跳转,我将父级(包含iframe)的高度设置为iframe高度,以便在执行此操作时保持总页面大小不变。

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. 元素是我的iframe。

The iframe has width: 100% style set and is inside a div with default styles (block). iframe具有宽度:100%样式集,并且位于具有默认样式(块)的div内。

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. 代码是jquery,并将div高度设置为iframe高度,然后将iframe设置为0高度,然后将iframe设置为内容高度。 If I remove the line that sets the iframe to 0 height, the iframe will get larger when needed, but never smaller. 如果我删除将iframe设置为0高度的行,iframe将在需要时变大,但从不变小。

This may not help you much but here is a function we have in what would be your iframe_contents.html page. 这可能对您没什么帮助,但这是我们在iframe_contents.html页面中的功能。 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: 它将尝试以一种自调整大小,跨浏览器,纯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"
    }
}

You could put calls to it in a resize() event or following an event that changes the height of your page. 您可以在resize()事件中或在更改页面高度的事件之后调用它。 The feature-testing in that method should separate out WebKit browsers and pick the correct height property. 该方法中的功能测试应该分离出WebKit浏览器并选择正确的height属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM