Domanda

Im messing around with this problem for about 2 days, still cant find a solution, so I have to ask here.. I have parallex website and I want to start autoscrolling whole website when user scrolls down (this point is functional) and when user scrolls up, the autoscroll stops.. But then, when user again scrolls down, it should start autoscrolling again in same speed (same timeout) as on the beggining, but everytime user scrolls up and down, the autoscroll get faster and faster...

Here is my code

    <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>
È stato utile?

Soluzione

Solved.. I just didnt know about function "clearTimeout"

So now it looks like this:

      <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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top