pierrebalian
9/25/2017 - 5:43 PM

Constant slow scroll, changes direction with your scroll

Constant slow scroll, changes direction with your scroll


      /*===================================================

          + PAGE

      ===================================================*/

      var page = {
        goingDown: false,
        stopScrolling: false,
        startScrollTimer: null,
        prevScroll: null,
        newScroll: null,
        scrollSpeed: 1,
        scrollInterval: 30
      };

      page.autoScroll = function() {
        if (!page.stopScrolling && $(window).width() > bp.l) {
          if (page.goingDown) {
            window.scrollBy(0, -page.scrollSpeed);
          } else {
            window.scrollBy(0, page.scrollSpeed);
          }
        }
      };

      page.watchScroll = function() {
        var scrollTop = $(window).scrollTop();

        page.autoScroll();
        // Stop automatic scroll when user is scrolling, and change direction if need be.
        // Adding/subtracting an extra 2 pixels for padding to prevent false positive on scroll.
        if (page.prevScroll + 2 < scrollTop || page.prevScroll - 2 > scrollTop) {
          page.stopScrolling = true;
          clearTimeout(page.startScrollTimer);
          page.startScrollTimer = setTimeout(function() {
            page.stopScrolling = false;
          }, 50);
          if (page.prevScroll + 30 < scrollTop) {
            page.goingDown = false;
          }
          if (page.prevScroll - 30 > scrollTop) {
            page.goingDown = true;
          }
        }
        page.prevScroll = scrollTop;
      };

      /*===================================================