IFrame自适应高度

来源:互联网 发布:linux自定义命令 编辑:程序博客网 时间:2024/06/05 17:18
想必很多做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;}


原创粉丝点击