javascript - 如何在父窗口调整大小时调整 iframe 的大小

标签 javascript css iframe resize window

我不认为这不是另一个“根据内容高度调整 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 就能做到这一点吗?

最佳答案

我创建了一个 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%;
}

关于javascript - 如何在父窗口调整大小时调整 iframe 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6634582/

相关文章:

iframe - 是否可以在 iframe(美国电子商务)中接受 PayPal 付款?

javascript - 固定可滚动 div 内的 anchor

javascript - 用于输入地址的自动完成文本框是个好主意吗?

javascript - React Router - 由于自定义路由组件,始终显示 404 页面

html - CSS 下拉菜单元素水平移动

jquery - 使用 jQuery 访问 Etherpad 上的内容 &lt;iframe&gt;

javascript - 如何从由DiagramBuilder.toJSON()返回的JSON创建AlloyUIDiagramBuilder?

javascript - 使用序号限制 d3.js x 轴中刻度线的数量

javascript - 如何从iframe标签中获取data-id值?

html - 如何在 wp8 webBrowser 控件 Iframe 中自动播放 Youtube 视频