如何给这段JS滚动代码添加向上和向下滚动按钮
发布网友
发布时间:2022-04-26 15:32
我来回答
共1个回答
热心网友
时间:2022-04-23 13:59
var Scroll = Class.create();
Scroll.prototype = {
initialize: function(element, height) {
this.element = $(element);
this.element.innerHTML += this.element.innerHTML;
this.height = height;
this.maxHeight = this.element.scrollHeight / 2;
this.counter = 0;
this.direction = true;
this.scroll();
this.timer = "";
this.element.onmouseover = this.stop.bind(this);
this.element.onmouseout = function() {
this.timer = setTimeout(this.scroll.bind(this), 1000);
}.bind(this);
},
up: function(elmUp) {
this.elmUp = $(elmUp);
this.elmUp.onclick = function() {
clearTimeout(this.timer);
this.direction = true;
this.timer = setTimeout(this.scroll.bind(this), 1000);
}.bind(this);
},
down: function(elmDown) {
this.elmDown = $(elmDown);
this.elmDown.onclick = function() {
clearTimeout(this.timer);
this.direction = false;
this.timer = setTimeout(this.scroll.bind(this), 1000);
}.bind(this);
},
scroll: function() {
if(this.direction){
if (this.element.scrollTop < this.maxHeight) {
this.element.scrollTop++;
this.counter++;
} else {
this.element.scrollTop = 0;
this.counter = 0;
}
}else{
if (this.element.scrollTop > 0) {
this.element.scrollTop--;
this.counter++;
} else {
this.element.scrollTop = this.maxHeight;
this.counter = 0;
}
}
if (this.counter < this.height) {
this.timer = setTimeout(this.scroll.bind(this), 20);
} else {
this.counter = 0;
this.timer = setTimeout(this.scroll.bind(this), 3000);
}
},
stop: function() {
clearTimeout(this.timer);
}
}
var myscroll = new Scroll("myscroll", 40);
myscroll.up("toTop");
myscroll.down("toBottom");
没仔细测,你试试看,替换对应部分,增加了个参数direction控制方向,true向上,false向下,两个方法up/down给你绑按钮。