如何给这段JS滚动代码添加向上和向下滚动按钮

functionpmjgd(){function$(element){if(arguments.length>1){for(vari=0,length=arguments... function pmjgd() {
function $(element){
if(arguments.length>1){
for(var i=0,length=arguments.length,elements=[];i<length;i++){
elements.push($(arguments[i]));
}
return elements;
}
if(typeof element=="string"){
return document.getElementById(element);
}else{
return element;
}
}
var Class={
create:function(){
return function(){
this.initialize.apply(this,arguments);
}
}
}
Function.prototype.bind=function(object){
var method=this;
return function(){
method.apply(object,arguments);
}
}
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.scroll();
this.timer="";
this.element.onmouseover=this.stop.bind(this);
this.element.onmouseout=function(){this.timer=setTimeout(this.scroll.bind(this),1000);}.bind(this);
},
scroll:function(){
if(this.element.scrollTop<this.maxHeight){
this.element.scrollTop++;
this.counter++;
}else{
this.element.scrollTop=0;
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);
}
展开
 我来答
百度网友e2d7ae3
推荐于2016-11-22 · TA获得超过2207个赞
知道大有可为答主
回答量:1287
采纳率:0%
帮助的人:1615万
展开全部
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给你绑按钮。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式