Scrolling can be detected by binding scroll event to window. To determine the scroll direction we need to compare the present scroll position with previous scroll position.

Program Flow

  1. Compare window.pageYOffset with lastScrollPosition - If it is greater the scroll direction is down or the scroll direction is up
  2. Store present window.pageYOffset to lastScrollPosition 

Script

$scope.lastScrollTop = 0;
$scope.direction = "";
angular.element($window).bind("scroll", function() {
      $scope.st = window.pageYOffset;
      if ($scope.st > $scope.lastScrollTop) {
          $scope.direction = "down";
      } else {
          $scope.direction = "up";
      }

      $scope.lastScrollTop = $scope.st;
      $scope.$apply();
      console.log($scope.direction);
 });

2 comments:

  1. How to determine the direction of scroll using angularJS. Scrolling can be detected on window events, Leaving things for essay writing not looking good you have a chance to make best dissertation help things perfect. Ever trying to determine the directions for events.

    ReplyDelete
  2. Hey! We are professional writing team, which are always ready to face with even the most complicated task. So once time we received a rather strange task from our client, who asked us to wrire for him article based on the idea you presented here on your blog "How to Determine the Direction of Scroll using AngularJS" and he emphasized the idea of conducting that paper in the so called form of review on it, so our professional writer agreed to do that difficult task. The result of his efforst you can find at cheap-papers writing platform where you can also order different paper, buy interesting presentations topics and receive professional writing assistance.

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts