当前位置:编程学习 > 网站相关 >>

框架 在ie ff 火狐浏览器 自适应高度

<iframe src="zhaoxi.net.htm" name="mainFrame" id="mainFrame" frameborder="0" scrolling="auto" style="width:817px;height:100%;"></iframe>
乍一看也没什么不妥,可是火狐就是不认这个height:100%,上Google搜索了一下,找到段脚本,改了改,就解决这个问题了.

思路:首先判断浏览器是IE还是火狐,然后专门针对火狐做单独的样式处理.
<script type="text/javascript">
     function resizeIframe(obj) {
         var Iframe = document.getElementByIdx_x(obj);
             if (Iframe){
                 if (Iframe.contentDocument){//ff
                     Iframe.style.height = Iframe.contentDocument.body.scrollHeight +60+'px';
                 }
                 else if(Iframe.document && Iframe.document.body.scrollHeight){//ie
                 Iframe.style.height = mainFrame.document.body.scrollHeight + 60 + 'px';
             }
         }
     }
</script>
在Iframe加载的时候就调用这个函数
<iframe src="zhaoxi.net.htm" name="mainFrame" id="mainFrame" onload="resizeIframe('mainFrame')" frameborder="0" scrolling="auto" style="width:817px;height:100%;"></iframe>

---------------------------------------------------------------

框架iframe高度自适应(兼容IE6,IE7,IE8,FF2,FF3)常见浏览器

今天下午,为了这个“小”问题,研究了半天,搜了半天,不过,现在网上的都是抄来抄去,试了十几种方案都不行,最后呢,还是搞定了,所以,赶紧写下来,备忘用吧!
网上找了很多文章,常见的如:
                    <iframe src="right1.html" name="mainFrame" id="mainFrame"  width="740px" frameborder="0" scrolling="no"></iframe>
                    <script type="text/javascript">
                    function reinitIframe(){
                    var iframe = document.getElementByIdx_x("mainFrame");
                    try{
                    var bHeight = iframe.contentWindow.document.body.scrollHeight;
                    var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
                    var height = Math.max(bHeight, dHeight);
                    iframe.height = height;
                    }catch (ex){}
                }
                window.setInterval("reinitIframe()", 200);
                </script>
但是测试发现,在火狐下,并不能收缩。需要刷新页面。
下面方法解决问题:
<iframe id="mainFrame" name="mainFrame" width="100%" onload="this.height=mainFrame.document.body.scrollHeight" frameborder="0" src="zhaoxi.org.htm"></iframe>
测试浏览器:IE6、IE7、IE8、FF3.0

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,