Adjust width and height of iframe to fit with content in it

前端 未结 30 2328
旧时难觅i
旧时难觅i 2020-11-22 02:09

I need a solution for auto-adjusting the width and height of an iframe to barely fit its content. The point is that t

相关标签:
30条回答
  • 2020-11-22 02:42
    <iframe src="hello.html" sandbox="allow-same-origin"
            onload="this.style.height=(this.contentWindow.document.body.scrollHeight+20)+'px';this.style.width=(this.contentWindow.document.body.scrollWidth+20)+'px';">
    </iframe>
    
    0 讨论(0)
  • 2020-11-22 02:43

    This is how I would do it (tested in FF/Chrome):

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
    function autoResize(iframe) {
        $(iframe).height($(iframe).contents().find('html').height());
    }
    </script>
    
    <iframe src="page.html" width="100%" height="100" marginheight="0" frameborder="0" onload="autoResize(this);"></iframe>
    
    0 讨论(0)
  • 2020-11-22 02:43

    Javascript to be placed in header:

    function resizeIframe(obj) {
            obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
          }
    

    Here goes iframe html code:

    <iframe class="spec_iframe" seamless="seamless" frameborder="0" scrolling="no" id="iframe" onload="javascript:resizeIframe(this);" src="somepage.php" style="height: 1726px;"></iframe>
    

    Css stylesheet

    >

    .spec_iframe {
            width: 100%;
            overflow: hidden;
        }
    
    0 讨论(0)
  • 2020-11-22 02:45

    I know the post is old, but I believe this is yet another way to do it. I just implemented on my code. Works perfectly both on page load and on page resize:

    var videoHeight;
    var videoWidth;
    var iframeHeight;
    var iframeWidth;
    
    function resizeIframe(){
        videoHeight = $('.video-container').height();//iframe parent div's height
        videoWidth = $('.video-container').width();//iframe parent div's width
    
        iframeHeight = $('.youtubeFrames').height(videoHeight);//iframe's height
        iframeWidth = $('.youtubeFrames').width(videoWidth);//iframe's width
    }
    resizeIframe();
    
    
    $(window).on('resize', function(){
        resizeIframe();
    });
    
    0 讨论(0)
  • 2020-11-22 02:46

    Here are several methods:

    <body style="margin:0px;padding:0px;overflow:hidden">
        <iframe src="http://www.example.com" frameborder="0" style="overflow:hidden;height:100%;width:100%" height="100%" width="100%"></iframe>
    </body>
    

    AND ANOTHER ALTERNATIVE

    <body style="margin:0px;padding:0px;overflow:hidden">
        <iframe src="http://www.example.com" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px" height="100%" width="100%"></iframe>
    </body>
    

    TO HIDE SCROLLING WITH 2 ALTERNATIVES AS SHOWN ABOVE

    <body style="margin:0px;padding:0px;overflow:hidden">
        <iframe src="http://www.example.com" frameborder="0" style="overflow:hidden;height:150%;width:150%" height="150%" width="150%"></iframe>
    </body>
    

    HACK WITH SECOND CODE

    <body style="margin:0px;padding:0px;overflow:hidden">
        <iframe src="http://www.example.com" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:150%;width:150%;position:absolute;top:0px;left:0px;right:0px;bottom:0px" height="150%" width="150%"></iframe>
    </body>
    

    To hide the scroll-bars of the iFrame, the parent is made "overflow:hidden" to hide scrollbars and the iFrame is made to go upto 150% width and height which forces the scroll-bars outside the page and since the body doesn't have scroll-bars one may not expect the iframe to be exceeding the bounds of the page. This hides the scrollbars of the iFrame with full width!

    source: set iframe auto height

    0 讨论(0)
  • 2020-11-22 02:47

    Cross-browser jQuery plug-in.

    Cross-bowser, cross domain library that uses mutationObserver to keep iFrame sized to the content and postMessage to communicate between iFrame and host page. Works with or without jQuery.

    0 讨论(0)
提交回复
热议问题