简体   繁体   English

具有固定宽度和动态高度的iframe

[英]iframe with fixed width and DYNAMIC height

Is it possible to use an iframe with a fixed width (.ex. 75%) and a dynamic height ? 是否可以使用固定宽度(例如75%)和动态高度的iframe? Want I want to achieve is that when the page that is loaded into the iframe, it will not be wider than I have specified, but the lenght needs to be according to the page its content. 我想要实现的是,当将页面加载到iframe中时,它的宽度不会比我指定的宽,但是长度必须根据页面的内容而定。 Is it a page with 5 lines text, the frame will be just big enough to display these 5 lines. 如果它是一个包含5行文本的页面,则框架将足够大以显示这5行。 Are we loading a large document with 1000 lines, the Iframe height will be automatically adjusted. 如果我们要加载1000行大文件,则iframe的高度会自动调整。

Prerequisites: 先决条件:

  • The url in the frame is on a different domain from the parent. 框架中的网址与父网址位于不同的域中。
  • The code should work on mobile phone browsers too. 该代码也应适用于手机浏览器。
  • Let's try to avoid jQuery if possible. 让我们尽量避免使用jQuery。 (to make the above faster) (以加快上述速度)

I know you'd like to avoid it, but it really shouldn't slow you down so much that it'd be a burden on your site. 我知道您想避免这种情况,但是它确实不应该使您减慢速度,以免给您的网站造成负担。 I've done far crazier things with jquery and it's handled it like a champ. 我用jquery做过更疯狂的事情,它像冠军一样处理。 When talking "dynamic" it's usually a safe bet that you will need to touch some javascript at some point :P 当谈论“动态”时,通常安全的选择是,您需要在某个时候触摸一些JavaScript:P

$(selector)[0].scrollHeight

As for making it dynamic? 至于使其动态化? You could setup an interval to adjust the height. 您可以设置一个间隔来调整高度。

Something like this: 像这样:

function setHeight(selector){ 
    var contentHeight = $(selector)[0].scrollHeight;
    $('#iframe-id').attr('height', contentHeight);
}

Then you either load it on page load or you wrap it in a setInterval. 然后,您可以在页面加载时加载它,也可以将其包装在setInterval中。

If it's just the jquery thing and you don't mind javascript, then this site could help you http://www.mattcutts.com/blog/iframe-height-scrollbar-example/ 如果这只是jquery的内容,而且您不介意使用javascript,那么此站点可以为您提供http://www.mattcutts.com/blog/iframe-height-scrollbar-example/

IFrame height can be manipulated using the page rendering code, or JavaScript...but there is no way for the iframe to dynamically resize based on content. 可以使用页面渲染代码或JavaScript来控制iframe的高度...但是iframe无法根据内容动态调整大小。

The link in mplungjan's comment takes you to a really good post about this subject. mplungjan评论中的链接将您带到关于该主题的非常好的帖子。

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

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