当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的动画函数怎么办?

转载于:https://www.cnblogs.com/zhujl/archive/2011/09/28/2193710.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值