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);
 });

0 comments:

Blogroll

Follow this blog by Email

Popular Posts