如何使iframe占用所有剩余空间

时间:2012-10-05 19:45:12

标签: html css iframe height

我有以下代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="res/favicon.ico" />
<title>My website</title>

<style type="text/css">
    .wrap {
    position:relative;
    margin:0 auto;
    width:900px;
    text-align:center;
}

#header, #footer {
    width:100%;
    float:left;
}

#footer {
    position:fixed;
    bottom:0;
    z-index:999999;
}
</style>

</head>
<body>
  <div id="header">
      <div class="wrap">
           <div class="logo">
                <h1>Header</h1>
           </div>
           <div>Menu goes here</div>
      </div>
 </div>

 <iframe src="http://www.cnn.com"></iframe>

  <div id="footer">
      <div class="wrap">
           <h2>Footer</h2>
      </div>
 </div>
</body>
</html>

如何设置iframe,以便它会占用页眉和页脚之间的所有剩余宽度和高度?

3 个答案:

答案 0 :(得分:7)

解决了它,需要一些javascript:)

直播示例:
http://simplestudio.rs/yard/framed/framed.html (离线)

<强> CODE:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="res/favicon.ico" />
<title>My website</title>

<style type="text/css">

#framed {

    position: relative;
    width: 100%;
    height: 100%;   

}

.wrap {
    position:relative;
    margin:0 auto;
    width:900px;
    text-align:center;
}

#header, #footer {
    width:100%;
    float:left;
}

#footer {
    position:fixed;
    bottom:0;
    z-index:999999;
}
</style>

    <script>

        var resize = setInterval(function(){chng_iframe_height('framed','header','footer')},500);

        function chng_iframe_height(ifrid,hid,fid) {

            var eheight = window.innerHeight;
            var ifrobj = document.getElementById(ifrid); 
            var header = document.getElementById(hid); 
            var footer = document.getElementById(fid); 
            var header_height = getComputedStyle(header).height;
            var footer_height = getComputedStyle(footer).height;

            var reserved_height = parseInt(header_height) + parseInt(footer_height);

            ifrobj.style.height = eheight - reserved_height +"px";     

        }
    </script>

</head>
<body onload="chng_iframe_height('framed','header','footer');">
  <div id="header">
      <div class="wrap">
           <div class="logo">
                <h1>Header</h1>
           </div>
           <div>Menu goes here</div>
      </div>
 </div>

 <iframe src="http://www.cnn.com" id="framed"></iframe>

  <div id="footer">
      <div class="wrap">
           <h2>Footer</h2>
      </div>
 </div>
</body>
</html>

基本上,我计算页眉和页脚渲染高度,得到window.innerHeight,从这些数字我知道页脚在页脚之间有多少px,所以我将该值分配给iframe和whoila它的工作原理......

另外我设置了setinterval来每隔半秒调用一次该函数,所以如果调整窗口大小,它几乎会立即更新iframe高度...

答案 1 :(得分:1)

尝试在iframe中添加一个类,或者在现场设置样式:

<iframe style="height:100%; width:100%;" src="http://www.cnn.com"></iframe>

也可以尝试:

<iframe height="100%" width="100%" src="http://www.cnn.com"></iframe>

另外,不要忘记删除body标签上的填充/边距,以便页眉和页脚一直显示:

body {
    margin:0;
    padding: 0;
}

编辑:

如果这不起作用,则必须添加固定的宽度和高度。

答案 2 :(得分:0)

我使用了.css()和$(window).height()当你使用$(window).height()时确保你有doctype为html

<script>
      var resize = setInterval(function(){chng_iframe_height('framed','header','footer')},500);

        function chng_iframe_height(ifrid,hid,fid) {

            var eheight = $(window).height();
            var ifrobj = document.getElementById(ifrid); 
            var header_height = $("#" + hid).css("height"); // getComputedStyle(header).height;
            var footer_height = $("#" + fid).css("height"); 

            var reserved_height = parseInt(header_height) + parseInt(footer_height);

            ifrobj.style.height = eheight - reserved_height +"px";     

        }
    </script>