【代码段】根据子页面自动设置iframe标签宽高

【代码段】根据子页面自动设置iframe标签宽高

本段代码功能 ,实现根据iframe加载的子页面的宽度以及高度,自动修改父页面中iframe标签的宽度以及高度 实现子页面的完全展示

配置

配置方法就是在iframe标签中绑定onload事件 οnlοad=”startInit(‘tab_content_desktop’,xxx);
xxx表示设置iframe标签的最小高度 比如说1000 就代表iframe标签高1000
tab_content_desktop iframe标签的id

<iframe  id="tab_content_desktop"  src="" frameborder="0" scrolling="no" onload="startInit('tab_content_desktop',1090);"></iframe>
(function(){
    var browserVersion = window.navigator.userAgent.toUpperCase();
    var isOpera = browserVersion.indexOf("OPERA") > -1 ? true : false;
    var isFireFox = browserVersion.indexOf("FIREFOX") > -1 ? true : false;
    var isChrome = browserVersion.indexOf("CHROME") > -1 ? true : false;
    var isSafari = browserVersion.indexOf("SAFARI") > -1 ? true : false;
    var isIE = (!!window.ActiveXObject || "ActiveXObject" in window);
    var isIE9More = (! -[1, ] == false);
    function reinitIframe(iframeId, minHeight) {
        try {
            var iframe = document.getElementById(iframeId);
            var minWidth=document.getElementById(iframeId).scrollWidth;
            var bHeight = 0;
            var bWidth = 0;
            if (isChrome == false && isSafari == false){
                bHeight = iframe.contentWindow.document.body.scrollHeight;
                bWidth = iframe.contentWindow.document.body.scrollWidth;}


            var dHeight = 0;
            var dWidth = 0;
            if (isFireFox == true){
                dHeight = iframe.contentWindow.document.documentElement.offsetHeight + 2;
                dWidth = iframe.contentWindow.document.documentElement.scrollWidth;}

            else if (isIE == false && isOpera == false){
                dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
                dWidth = iframe.contentWindow.document.documentElement.scrollWidth;
            }

            else if (isIE == true && isIE9More) {//ie9+
                var heightDeviation = bHeight - eval("window.IE9MoreRealHeight" + iframeId);
                var widthDeviation = dWidth - eval("window.IE9MoreRealWidth" + iframeId);
                if (heightDeviation == 0) {
                    bHeight += 3;
                } else if (heightDeviation != 3) {
                    eval("window.IE9MoreRealHeight" + iframeId + "=" + bHeight);
                    eval("window.IE9MoreRealWidth" + iframeId + "=" + dWidth);
                    bHeight += 3;
                }
            }
            else//ie[6-8]、OPERA
                bHeight += 3;
            var height = Math.max(bHeight, dHeight);
            var width = Math.max(bWidth, dWidth);
            if (height < minHeight) height = minHeight;
            if (width < minWidth) width = minWidth;
            console.log("height==="+height);
            console.log("width==="+width);
            iframe.style.height = height + "px";
            iframe.style.width = width + "px";
            return;
        } catch (ex) { }
    }
    function startInit(iframeId, minHeight) {

        eval("window.IE9MoreRealHeight" + iframeId + "=0");
        reinitIframe(iframeId, minHeight);
    }
    window.startInit=startInit;
})()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值