IFrame自适应高度

想必很多做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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值