iframe高度自适应问题

本文介绍了在使用HTML静态页面和Bootstrap框架时遇到的iframe高度自适应问题,并分享了一个通过JavaScript实现的解决方案,确保iframe能够根据内容自动调整高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天写了一下html静态页,遇到iframe高度自适应问题。本来是使用bootstrap的,看它上面的文档说直接使用它们给好样式就可以了。但是并没有达到要的效果。
最后使用了这个中方法解决了。

<div class="col-lg-10 col-md-10">
            <div class="embed-responsive embed-responsive-4by3" id="mainContent">
                <iframe class="embed-responsive-item" id="mainFrame" src="home.html" onload="javascript:iframeAutoHeight();"></iframe>
            </div>
          </div>

这里是使用js实现的,代码如下

        //iframe 高度自适应
        function iframeAutoHeight(){
            var iframe=document.getElementById("mainFrame");
            if(navigator.userAgent.indexOf("MSIE")>0||navigator.userAgent.indexOf("rv:11")>0||navigator.userAgent.indexOf("Firefox")>0){
                iframe.height=iframe.contentWindow.document.body.scrollHeight;
            }else{
                iframe.height=iframe.contentWindow.document.documentElement.scrollHeight;
            }
            iframe.style.cssText="height:"+iframe.height+"px;";
            $("#mainContent").css("height",iframe.height+"px");
            $("#menu").css("height",iframe.height+"px");
        }

注:无关代码请忽略

各位大神有什么好的解决方案,可以劳烦留言一下给小弟吗,万分感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值