JS平滑移动滚动条

/**
 * 平滑移动滚动条
 * 如果是IE就直接跳吧.慢死了.
 */
var phydgdt = function(){
	this.w = document.body.scrollWidth;	//滚动条的总宽度
	this.h = document.body.scrollHeight;	//滚动条的总高度
	this.x = document.body.scrollLeft;	//滚动条横向位置
	this.y = document.body.scrollTop;	//滚动条纵向位置
	this.id = -1;
	this.list = [];
	this.isIE = !+ [ 1, ] ? true : false;
}
phydgdt.prototype = {
	start : function(type,to){
		if(this.id != -1){
			this.list.push({type:type,to:to});
			return "等待";
		}
		this.bool = to > (type === "x" ? this.x : this.y);	//是用加还是用减
		this.sd = 15;	//每次移动的大小
		this.time = 1;
		if(to < 0)to = 0;
		if(this.isIE){		//IE直接条
			if(this.type === "x"){
				scrollTo(to,this.y);
			}else{
				scrollTo(this.x,to);
			}
			fddiv.update();
			this.stop();
			return;
		}
		this.id = setInterval((function(param) {
			return function() {
				if(param.type === "x"){
					if(param.bool){
						param.x += param.sd;
						if(param.x < to){
							scrollTo(param.x,param.y);
						}else{
							scrollTo(to,param.y);
							param.stop();
						}
					}else{
						param.x -= param.sd;
						if(param.x > to){
							scrollTo(param.x,param.y);
						}else{
							scrollTo(to,param.y);
							param.stop();
						}
					}
				}else{
					if(param.bool){
						param.y += param.sd;
						if(param.y < to){
							scrollTo(param.x,param.y);
						}else{
							scrollTo(param.x,to);
							param.stop();
						}
					}else{
						param.y -= param.sd;
						if(param.y > to){
							scrollTo(param.x,param.y);
						}else{
							scrollTo(param.x,to);
							param.stop();
						}
					}
				}
				fddiv.update();
			}
		})(this), this.time)
	},
	stop : function(){
		if(this.id != -1){
			clearInterval(this.id);
			this.id = -1;
		}
		this.x = document.body.scrollLeft;	//滚动条横向位置
		this.y = document.body.scrollTop;	//滚动条纵向位置
		if(this.list.length > 0){
			var obj = this.list[0];
			this.list = this.list.slice(0,0).concat(this.list.slice(1,this.list.length));
			this.start(obj.type,obj.to);
		}
	},
	gotoObj : function( id ){
		if(id){
			var el = document.getElementById(id);
			var tl = el.offsetTop - 8;
			this.start("y",tl);
		}
	}
};
//使用方法
var pdt = new phydgdt();
pdt.gotoObj("对象ID");

你可能感兴趣的:(JS定位滚动条,JS滚动位置,JS平滑移动,JS平滑移动滚动条)