【问题标题】:How to resize iframe when parent window resizes父窗口调整大小时如何调整iframe大小
【发布时间】:2011-10-01 20:26:16
【问题描述】:

我不认为这不是另一个“根据内容高度调整 iframe 大小”的问题。

我实际上想根据父窗口的调整大小动态调整 iframe 的大小。对于 JS Fiddle 粉丝,我有一个例子 here

对于那些想在 SO 上查看代码的人:

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

</div>

<div id="block"></div>

<div id="header"></div>

<div id="footer"></div>

CSS:

body {
margin: 0px;
padding-top: 78px;
padding-right: 0px;
padding-bottom: 25px;
padding-left: 0px;
min-height: 0px;
height: auto;
text-align: center;
background-color: lightblue;
overflow:hidden;
}

div#header {
top: 0px;
left: 0px;
width: 100%;
height: 85px;
min-width: 1000px;
overflow: hidden;
background-color: darkblue;
}

div#footer {
bottom: 0px;
left: 0px;
width: 100%;
height: 25px;
min-width: 1000px;
background-color: darkblue;
}

iframe#frame2 {

margin: 40px;
position: fixed;
top: 80px;
left: 0px;
width: 200px;
bottom: 25px;
min-width: 200px;
}

div#block {

background-color: lightgreen;
margin: 40px;
position: fixed;
top: 80px;
left: 350px;
width: 200px;
bottom: 25px;
min-width: 200px;
}

@media screen {
body > div#header {
position: fixed;
}
body > div#footer {
position: fixed;
}
}

那里可能有一些奇怪的 CSS - 我从实际页面拼凑而成。道歉。

如您所见,当您调整窗口大小时,绿色 div 会相应地动态更改高度。我想知道的是,这是否可以通过 div 左侧的 iframe 来完成。

单靠 CSS 能做到这一点吗?

【问题讨论】:

  • 您的 JsFiddle 链接无效。
  • 谢谢我修好了。 JSFiddle 链接现在应该可以工作了...

标签: javascript css iframe resize window


【解决方案1】:

这对我有用:

div#content iframe {width: 100%} 

【讨论】:

    【解决方案2】:

    我创建了一个new jsfiddle,它可以为您提供原始 css 中所需的内容。我没有广泛地测试跨浏览器,尤其是在 IE 中。我预计会在 IE8 和 9 中得到支持,但会犹豫是否说 7 可以正常工作。

    相关变化:

        /* 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%;
    }
    

    【讨论】:

      【解决方案3】:

      我认为这可以满足您的需求。

      首先我将 iframe 包裹在一个 div 中,并将 iframe 的宽度和高度设置为 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% }
      

      然后我添加了以下 jQuery 代码。

      jsFiddle

      $(function() {
          var widthRatio = $('#frameContainer').width() / $(window).width();
          $(window).resize(function() {
              $('#frameContainer').css({width: $(window).width() * widthRatio});
          }); 
      });
      

      【讨论】:

      • 这也有效,但我选择了另一个答案,因为它是纯 CSS,但我承认使用 Jquery 可能有助于兼容性
      • 对我来说,(只是测试 jsFiddles)这是两者中唯一有效的选项,即使在 Chrome 中也是如此。
      【解决方案4】:

      您可以将 iframe 元素的宽度和高度设置为基于百分比。这是一个宽度为 75% 的示例,当您增加/减少浏览器窗口的宽度时会动态变化:http://jsfiddle.net/fallen888/pkjEB/

      【讨论】:

      • 这不是答案。我需要它的行为类似于示例中称为block 的div。这个div 的顶部边缘和底部边缘始终与顶部和底部的距离相同(以像素为单位)。这与将元素设置为高度的百分比不同。
      猜你喜欢
      • 2014-01-01
      • 2023-03-24
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多