make iframe height dynamic based on content inside- JQUERY/Javascript

前端 未结 20 1996
情书的邮戳
情书的邮戳 2020-11-22 07:42

I am loading an aspx web page in an iframe. The content in the Iframe can be of more height than the iframe\'s height. The iframe should not have scroll bars.

I have

相关标签:
20条回答
  • 2020-11-22 08:10

    you could also add a repeating requestAnimationFrame to your resizeIframe (e.g. from @BlueFish's answer) which would always be called before the browser paints the layout and you could update the height of the iframe when its content have changed their heights. e.g. input forms, lazy loaded content etc.

    <script type="text/javascript">
      function resizeIframe(iframe) {
        iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";
        window.requestAnimationFrame(() => resizeIframe(iframe));
      }
    </script>  
    
    <iframe onload="resizeIframe(this)" ...
    

    your callback should be fast enough to have no big impact on your overall performance

    0 讨论(0)
  • 2020-11-22 08:11

    Add this to the iframe, this worked for me:

    onload="this.height=this.contentWindow.document.body.scrollHeight;"
    

    And if you use jQuery try this code:

    onload="$(this).height($(this.contentWindow.document.body).find(\'div\').first().height());"
    
    0 讨论(0)
  • 2020-11-22 08:11
    jQuery('.home_vidio_img1 img').click(function(){
        video = '<iframe src="'+ jQuery(this).attr('data-video') +'"></iframe>';
        jQuery(this).replaceWith(video);
    });
    
    jQuery('.home_vidio_img2 img').click(function(){
        video = <iframe src="'+ jQuery(this).attr('data-video') +'"></iframe>;
        jQuery('.home_vidio_img1 img').replaceWith(video);
        jQuery('.home_vidio_img1 iframe').replaceWith(video);
    });
    
    jQuery('.home_vidio_img3 img').click(function(){
        video = '<iframe src="'+ jQuery(this).attr('data-video') +'"></iframe>';
        jQuery('.home_vidio_img1 img').replaceWith(video);
        jQuery('.home_vidio_img1 iframe').replaceWith(video);
    });
    
    jQuery('.home_vidio_img4 img').click(function(){
        video = '<iframe src="'+ jQuery(this).attr('data-video') +'"></iframe>';
        jQuery('.home_vidio_img1 img').replaceWith(video);
        jQuery('.home_vidio_img1 iframe').replaceWith(video);
    });
    
    0 讨论(0)
  • 2020-11-22 08:11

    Rather than using javscript/jquery the easiest way I found is:

    <iframe style="min-height:98vh" src="http://yourdomain.com" width="100%"></iframe>

    Here 1vh = 1% of Browser window height. So the theoretical value of height to be set is 100vh but practically 98vh did the magic.

    0 讨论(0)
  • 2020-11-22 08:15

    A slightly improved answer to Aristos...

    <script type="text/javascript">
      function resizeIframe(iframe) {
        iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";
      }
    </script>  
    

    Then declare in your iframe as follows:

    <iframe onload="resizeIframe(this)" ...
    

    There are two minor improvements:

    1. You don't need to get the element via document.getElementById - as you already have it in the onload callback.
    2. There's no need to set the iframe.height = "" if you're going to reassign it in the very next statement. Doing so actually incurs an overhead as you're dealing with a DOM element.

    Edit: If the content in the frame is always changing then call:

    parent.resizeIframe(this.frameElement); 
    

    from within the iframe after the update. Works for same origin.

    Or to auto detect:

      // on resize
      this.container = this.frameElement.contentWindow.document.body;
    
      this.watch = () => {
        cancelAnimationFrame(this.watcher);
    
        if (this.lastScrollHeight !== container.scrollHeight) {
          parent.resizeIframeToContentSize(this.frameElement);  
        }
        this.lastScrollHeight = container.scrollHeight;
        this.watcher = requestAnimationFrame(this.watch);
      };
      this.watcher = window.requestAnimationFrame(this.watch);
    
    0 讨论(0)
  • 2020-11-22 08:15

    The simple solution is to measure the width and height of the content area, and then use those measurements to calculate the bottom padding percentage.

    In this case, the measurements are 1680 x 720 px, so the padding on the bottom is 720 / 1680 = 0.43 * 100, which comes out to 43%.

    .canvas-container {    
        position: relative;
        padding-bottom: 43%; // (720 ÷ 1680 = 0.4286 = 43%)
        height: 0;
        overflow: hidden;   
    }
    
    .canvas-container iframe {    
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;   
    }
    
    0 讨论(0)
提交回复
热议问题