jQuery / JS当用户向上滚动并继续向上滚动时,使自动滚动停止

时间:2014-03-30 15:00:36

标签: javascript jquery autoscroll

我解决这个问题大约2天,仍然无法找到解决方案,所以我必须在这里问...我有parallex网站,我想在用户向下滚动时开始自动滚动整个网站(这一点是功能的)当用户向上滚动时,自动滚动停止..但是,当用户再次向下滚动时,它应该以与开始时相同的速度(相同的超时)再次开始自动滚动,但每次用户向上和向下滚动时,自动滚动变得更快,更快...

这是我的代码

    <script>
    var act = 0;
    var y = 0;
    var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
    $(window).bind(mousewheelevt, function(e){

        var evt = window.event || e //equalize event object     
        evt = evt.originalEvent ? evt.originalEvent : evt; //convert to originalEvent if possible               
        var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta //check for detail first, because it is used by Opera and FF

        if(delta > 0) {
           console.log('nahoru stop y=0 act=0');
           y = 0;
           pageScrollStart(y);
           act = 0;
        }
        else{

            if(act==1)
            {
              console.log('dolu aktivni nic');
            }
            else
            {
              console.log('dolu neaktivni start y=1 act=1');
              y = 1;
              pageScrollStart(y);
              act = 1;
            }
          } 

    });
            function pageScrollStart() {
                window.scrollBy(0,y);
                scrolldelay = setTimeout('pageScrollStart()',50);
            };

    </script>

1 个答案:

答案 0 :(得分:0)

解决了..我只是不知道函数&#34; clearTimeout&#34;

所以现在它看起来像这样:

      <script>
    var act = 0;
    var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
    $(window).bind(mousewheelevt, function(e){

        var evt = window.event || e //equalize event object     
        evt = evt.originalEvent ? evt.originalEvent : evt; //convert to originalEvent if possible               
        var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta //check for detail first, because it is used by Opera and FF

        if(delta > 0) {
           pageScrollStop();
          console.log('nahoru stop');
          act = 0;
        }
        else{
          if(act !== 1)
           {

              pageScrollStart();
              console.log('dolu start');
              act = 1;
           }
          } 

    });
            function pageScrollStart() {
                window.scrollBy(0,1);
                scrolldelay = setTimeout('pageScrollStart()',50);
            };
            function pageScrollStop(){
              clearTimeout(scrolldelay);
            }
    </script>