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");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值