关于文字滚动的问题

下面是文字向上滚动的代码,这里面为什么要设置controls={}, 而且后面的元素选择也用到了。还有里面的this.init=function(){}为什么要赋值给this.init。下面的new scrollTxt().init();是什么意思呢?初学者多多指教
function  scrollTxt(){
    var controls={},
        values={},
        t1=200, /*播放动画的时间*/
        t2=2000, /*播放时间间隔*/
        si;
    controls.rollWrap=$("#roll-wrap");
    controls.rollWrapUl=controls.rollWrap.children();
    controls.rollWrapLIs=controls.rollWrapUl.children();
    values.liNums=controls.rollWrapLIs.length;
    values.liHeight=controls.rollWrapLIs.eq(0).height();
    values.ulHeight=controls.rollWrap.height();
    this.init=function(){
        autoPlay();
        pausePlay();
    }
    /*滚动*/
    function play(){
        controls.rollWrapUl.animate({"margin-top" : "-"+values.liHeight}, t1, function(){
            $(this).css("margin-top" , "0").children().eq(0).appendTo($(this));
        });
    }
    /*自动滚动*/
    function autoPlay(){
        /*如果所有li标签的高度和大于.roll-wrap的高度则滚动*/
        if(values.liHeight*values.liNums > values.ulHeight){
            si=setInterval(function(){
                play();
            },t2);
        }
    }
    /*鼠标经过ul时暂停滚动*/
    function pausePlay(){
        controls.rollWrapUl.on({
            "mouseenter":function(){
                clearInterval(si);
            },
            "mouseleave":function(){
                autoPlay();
            }
        });
    }
}
new scrollTxt().init();

成长前端初学者
浏览 1192回答 1
1回答

林逸舟丶

1.为什么var controls={}:    因为通过定义controls为一个对象,才能在后面对其属性进行赋值:  controls.rollWrap=$("#roll-wrap");   controls.rollWrapUl=controls.rollWrap.children();   controls.rollWrapLIs=controls.rollWrapUl.children();    后面values={}同理。2.赋值给this.init:    this指向scrollTxt(),通过this.init赋值一个方法(function),才能有最后的new scrollTxt().init()执行方法。3.new scrollTxt().init()即执行init这个方法function(){     autoPlay();     pausePlay(); }
打开App,查看更多内容
随时随地看视频慕课网APP