当0遇上0,拼人品的时候到了

话说取值习惯用a||b,当0遇上0,这句话就是一句废话!

我的问题是这样的:

首先页面有滚动条,滚动条位于初始位置(最顶上),然后我点击一个按钮,缓动到底部,这个时候,就会发生见鬼的0与0的对决。

//jQuery代码
function toBottom(){
	var el = null,
		scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight),
		clientHeight = Math.min(document.documentElement.clientHeight, document.body.clientHeight);
	//你懂的,下面这段等价于0||0
	if(document.documentElement.scrollTop){
		el = document.documentElement;
	}else{
		el = document.body;
	}
	$(el).animate({scrollTop: scrollHeight - clientHeight}, 500);
}

看到问号没,因为是0||0,根本不知道动画的对象是document.documentElement还是document.body。

所以怎么办呢,我测试过几个浏览器,结果是这样的

Chrome, Safari : document.body

  IE, FF, Opera : document.documentElement

难道逼我判断浏览器?这样做实在太蛋疼了,我没办法了,求各位赐教!

今天请教同事,还有个办法,用window.scrollTo(x, y):

function toBottom(){
	var scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight),
		clientHeight = Math.min(document.documentElement.clientHeight, document.body.clientHeight);
	window.scrollTo(0, scrollHeight - clientHeight)
}

但这样问题又来了,没动画效果。。。我还是想用jQuery的动画函数怎么办?

posted @ 2011-09-28 01:09  越己  阅读(2834)  评论(15编辑  收藏  举报