页面加载时就确定函数实现

我们知道使用js获取浏览器的信息时经常涉及浏览器的兼容性.例如获取浏览器的滚动条

// Cross browser gets the position of scroll
com.whuang.hsj.getScroll=function(){
    return {
        top:document.documentElement.scrollTop || document.body.scrollTop,
        left:document.documentElement.scrollLeft || document.body.scrollLeft,
        height:document.documentElement.scrollHeight ||document.body.scrollHeight
    }
}

 上面的方法每次执行时都得判断,所以我进行了如下优化:

// Cross browser gets the position of scroll
com.whuang.hsj.getScroll=(function(){
	if(document.documentElement.scrollTop!=='undefined'&&document.documentElement.scrollTop!==0)
	{
		return function(){
			return {
				top:document.documentElement.scrollTop,
				left:document.documentElement.scrollLeft,
				height:document.documentElement.scrollHeight
			};
		};
	}else return function(){
		return {
			top:document.body.scrollTop,
			left:document.body.scrollLeft,
			height:document.body.scrollHeight
		};
	};
    
})();

 核心思想:在初始化时就确定函数的实现方式.

但是在使用过程中出现了问题,因为它的判断条件是:

if(document.documentElement.scrollTop!=='undefined'&&document.documentElement.scrollTop!==0)

但是在页面初始化时这个判断的依据是有问题的,不是实时的.在火狐中就有问题.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值