您的位置:首页 > Web前端 > JQuery

IFrame自适应高度

2011-11-07 12:55 155 查看
想必很多做web开发的人都遇到过iframe内容有滚动条情况,解决方案也是一搜一大堆。自己在遇到一个高人的方案后,稍微做了下调整,支持IE6、IE8、Chrome、Firefox和Opera,感觉比较理想,在此与各位分享,希望共同进步。

内容不多,也不复杂,不做过多说明。前提是需要需要jquery库,在iframe的onload事件中指定调用该函数即可。

function autoHeight(iframe) {
var bHeight;
var dHeight;
var zeroHeight = 10;
var initHeight = 450;
if($.browser.msie){
bHeight = iframe.contentWindow.document.body.scrollHeight;
iframe.height = zeroHeight;
dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
}
else if($.browser.mozilla || $.browser.opera){
dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
iframe.height = zeroHeight;
bHeight = iframe.contentWindow.document.body.scrollHeight;
}
else if($.browser.webkit){
iframe.height = zeroHeight;
dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
bHeight = iframe.contentWindow.document.body.scrollHeight;
}
else {
iframe.height = zeroHeight;
dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
bHeight = iframe.contentWindow.document.body.scrollHeight;
}

var height = bHeight > dHeight ? bHeight : dHeight;
//alert(bHeight + ":" + dHeight + "=>" + height);
if(height < initHeight){
height = initHeight;
}
iframe.height = height;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息