Skip to content

Instantly share code, notes, and snippets.

@Elevista
Created June 4, 2016 14:04
Show Gist options
  • Save Elevista/465f98499ed7cb8528e3f724bbc24dec to your computer and use it in GitHub Desktop.
Save Elevista/465f98499ed7cb8528e3f724bbc24dec to your computer and use it in GitHub Desktop.
AngularJS onScroll directive
;(function (window, _, $, angular, undefined) {
var module = angular.module("app");
module.directive("onScroll", [function () {
var previousScroll = 0;
var link = function ($scope, $element, attrs) {
$element.bind('scroll', function (evt) {
var currentScroll = $element.scrollTop();
$scope.$eval(attrs["onScroll"], {$event: evt, $direct: currentScroll > previousScroll ? 1 : -1});
previousScroll = currentScroll;
});
};
return {
restrict: "A",
link: link
};
}]);
})(window, _, jQuery, angular);

About Directive

Bind on scroll event angular way.

Usage

<div on-scroll="scrollHdlr($direct,$event)">
angular.module("app").controller('ctrl', function ($scope) {
	$scope.scrollHdlr = function ($event, $direct) {
		console.log("Scrolling " + $direct > 1 ? "down" : "up", $event);
	};
});
@mihaisavezi
Copy link

Not working unless you have jQuery. To access currentScroll without jQuery you need to acess scrollTop as a property not a method i.e. current = $element[0].scrollTop;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment