Skip to content

Instantly share code, notes, and snippets.

@reu
Created October 10, 2013 04:42
Show Gist options
  • Save reu/22988a9bb3cb12fba9ed to your computer and use it in GitHub Desktop.
Save reu/22988a9bb3cb12fba9ed to your computer and use it in GitHub Desktop.
Angular.js autoscroll directive.
angular.module("autoscroll", [])
.service("scroller", function() {
this.defaultTime = 500;
this.scroll = function(element, height, time) {
element.animate({
scrollTop: height
}, time || this.defaultTime);
}
this.scrollUp = function(element, time) {
this.scroll(element, 0, time);
}
this.scrollDown = function(element, time) {
this.scroll(element, element.prop("scrollHeight"), time);
}
})
.directive("autoScroll", ["$timeout", "scroller", function($timeout, scroller) {
return function(scope, element, attrs) {
var direction = attrs.autoScroll;
scope.$watch(function() {
return element.children().length;
}, function() {
switch(direction) {
case "up":
scroller.scrollUp(element, 500);
break;
case "down":
scroller.scrollDown(element, 500);
break;
default:
throw "You must specify an 'up' or 'down' autoscroll direction.";
}
});
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment