How to resize iframe when parent window resizes

前端 未结 4 1317
忘掉有多难
忘掉有多难 2021-01-31 20:35

I don\'t think this is not another \"resize iframe according to content height\" question.

I actually want to resize the iframe dynamically according to a resize of the

相关标签:
4条回答
  • 2021-01-31 21:00

    You can set the width and height of the iframe element to be percentage-based. Here's an example where width is 75% and will dynamically change when you increase/decrease the width of your browser window: http://jsfiddle.net/fallen888/pkjEB/

    0 讨论(0)
  • 2021-01-31 21:09

    This worked for me:

    div#content iframe {width: 100%} 
    
    0 讨论(0)
  • 2021-01-31 21:15

    I created a new jsfiddle that gets you what you need in raw css. I didn't test cross-browser extensively, particularly in IE. I would anticipate support in IE8 and 9, but would be hesitant to say that 7 would work without hiccups.

    The relevant changes:

        /* This contains the iframe and sets a new stacking context */
    div#content {
        position: fixed;
        top: 80px;
        left: 40px;
        bottom: 25px;
        min-width: 200px;
        background: black; 
            /* DEBUG: If the iframe doesn't cover the whole space,
               it'll show through as black. */
    }
    
        /* Position the iframe inside the new stacking context
           to take up the whole space */
    div#content iframe {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        height: 100%;
        width: 100%;
    }
    
    0 讨论(0)
  • 2021-01-31 21:17

    I think this does what you're after.

    First I wrapped the iframe in a div, and set the iframe's width and height to be 100%.

    HTML

    <div id="frameContainer"><iframe src="http://www.apple.com" name="frame2" id="frame2" frameborder="0" marginwidth="0" marginheight="0" scrolling="auto" onload="" allowtransparency="false"></iframe></div>
    

    CSS

    #frameContainer {
    
        margin: 40px;
        position: fixed;
        top: 80px;
        left: 0px;
        width: 200px;
        bottom: 25px;
        min-width: 200px;
    
    }
    
    iframe#frame2 { width: 100%; height:100% }
    

    Then I added the following jQuery code.

    jsFiddle

    $(function() {
        var widthRatio = $('#frameContainer').width() / $(window).width();
        $(window).resize(function() {
            $('#frameContainer').css({width: $(window).width() * widthRatio});
        }); 
    });
    
    0 讨论(0)
提交回复
热议问题