简体   繁体   English

使用Jquery将iframe大小调整为内容

[英]Resize iframe to content with Jquery

I'm trying to resize an iframe dynamicly to fit its content. 我正在尝试动态调整iframe的大小以适应其内容。 To do so I have a piece of code: 为此,我有一段代码:

$("#IframeId").height($("#IframeId").contents().find("html").height());​

It doesnt work. 它不起作用。 Is it because of cross-domain issue? 是因为跨域问题吗? How do I get it to fit? 我如何使它适合? Please take a look at Fiddle: JsFiddle 请看看Fiddle: JsFiddle

ps I have set the html and body of the link height:100%; ps我已设置链接height:100%;的html和正文height:100%;

You just need to apply your code on the iframe load event, so the height is already known at that time, code follows: 您只需要在iframe load事件上应用代码,因此当时已知高度,代码如下:

$("#IframeId").load(function() {
    $(this).height( $(this).contents().find("body").height() );
});

See working demo . 看工作演示 This demo works on jsfiddle as I've set the iframe url to a url in the same domain as the jsfiddle result iframe, that is, the fiddle.jshell.net domain. 这个演示适用于jsfiddle,因为我已经将iframe url设置为与jsfiddle结果iframe在同一域中的url,即fiddle.jshell.net域。

UPDATE : 更新
@Youss: It seems your page for a strange reason don't get the body height right, so try using the height of the main elements instead, like this: @Youss:看起来你的页面由于一个奇怪的原因没有得到正确的身高,所以尝试使用主要元素的高度,如下所示:

$(document).ready(function() {
    $("#IframeId").load(function() {
            var h = $(this).contents().find("ul.jq-text").height();
            h += $(this).contents().find("#form1").height();
            $(this).height( h );
        });
});

Not sure why @Nelson's solution wasn't working in Firefox 26 (Ubuntu), but the following Javascript-jQuery solution seems to work in Chromium and Firefox. 不知道为什么@Nelson的解决方案不适用于Firefox 26(Ubuntu),但以下Javascript-jQuery解决方案似乎适用于Chromium和Firefox。

/**
 * Called to resize a given iframe.
 *
 * @param frame The iframe to resize.
 */
function resize( frame ) {
  var b = frame.contentWindow.document.body || frame.contentDocument.body,
      cHeight = $(b).height();

  if( frame.oHeight !== cHeight ) {
    $(frame).height( 0 );
    frame.style.height = 0;

    $(frame).height( cHeight );
    frame.style.height = cHeight + "px";

    frame.oHeight = cHeight;
  }

  // Call again to check whether the content height has changed.
  setTimeout( function() { resize( frame ); }, 250 );
}

/**
 * Resizes all the iframe objects on the current page. This is called when
 * the page is loaded. For some reason using jQuery to trigger on loading
 * the iframe does not work in Firefox 26.
 */
window.onload = function() {
  var frame,
      frames = document.getElementsByTagName( 'iframe' ),
      i = frames.length - 1;

  while( i >= 0 ) {
    frame = frames[i];
    frame.onload = resize( frame );

    i -= 1;
  }
};

This continually resizes all iframes on a given page. 这会不断调整给定页面上的所有iframe的大小。

Tested with jQuery 1.10.2 . jQuery 1.10.2测试。

Using $('iframe').on( 'load', ... would only work intermittently. Note that the size must initially be set to 0 pixels in height if it is to shrink below the default iframe height in some browsers. 使用$('iframe').on( 'load', ...只会间歇性地工作。请注意,如果要在某些浏览器中缩小到默认iframe高度以下,则最初必须将大小设置为0像素。

What you can do is the following: 你能做的是以下几点:

  • Within the iFrame use document.parent.setHeight(myheight) to set the height within the iFrame to the parent. 在iFrame中使用document.parent.setHeight(myheight)将iFrame中的高度设置为父级。 Which is allowed since it is a child control. 这是允许的,因为它是儿童控制。 Call a function from the parent. 从父级调用函数。

  • Within the parent you make a function setHeight(iframeheight) which resizes the iFrame. 在父级中,您创建了一个函数setHeight(iframeheight),用于调整iFrame的大小。

Also see: 另见:
How do I implement Cross Domain URL Access from an Iframe using Javascript? 如何使用Javascript从Iframe实现跨域URL访问?

Just do it on the HTML tag, works perfect 只需在HTML标签上执行此操作即可

$("#iframe").load(function() {
    $(this).height( $(this).contents().find("html").height() );
});

As the answer to the question use an already outdated jquery (load has been deprecated and replaced with .on('load',function(){}), below is the latest code for the answer in the question. 由于问题的答案使用已经过时的jquery(已经弃用了load并用.on('load',function(){}替换),下面是问题答案的最新代码。

Note that I use the scrollHeight and scrollWidth, which I think will load much nicer than using Height and Width like the answer provided. 请注意,我使用scrollHeight和scrollWidth,我认为它会比使用Height和Width更好地加载,就像提供的答案一样。 It will totally fit, without scroll anymore. 它将完全适合,不再滚动。

$("#dreport_frame").on('load',function(){

  var h = $('#dreport_frame').contents().find("body").prop('scrollHeight');
  var w = $('#dreport_frame').contents().find("body").prop('scrollWidth');

  $('#dreport_frame').height(h);
  $('#dreport_frame').width(w);
})

Adjust height of an iframe, on load and resize, based on its body height. 调整iframe的高度,加载并根据其身高调整大小。

var iFrameID = document.getElementById('iframe2');
var iframeWin = iFrameID.contentWindow;

var eventList = ["load", "resize"];
for(event of eventList) {
    iframeWin.addEventListener(event, function(){ 
        if(iFrameID) {
            var h = iframeWin.document.body.offsetHeight  + "px";
            if(iFrameID.height == h) {
                return false;
            }

            iFrameID.height = "";
            iFrameID.height = iframeWin.document.body.offsetHeight  + "px";

        }   
    })  
} 

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

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